如何在Java中创建对象列表

6

我有一个名为MarkerCustom的类文件。MarkerCustom有一个构造函数,它接受三个不同类型的变量。

public MarkerCustom(int myInt, String, myString, BitmapData myBitmap) {

从我的主活动中,我想加载一个GLSurface视图,它将接受一个MarkerCustom实例的ArrayList,并将要加载到GLSurface中的数据传递给每个MarkerCustom构造函数的实例。

我们将称这个ArrayList为myMarkers;

我需要myMarkers看起来像这样:

myMarkers[0] = [1, "hello", bitMapData1]
myMarkers[1] = [66, "johnHandy", bitmapData2]

我对Java还比较陌生,它的类型转换等方面让我有些困惑,因为我之前使用的是AS3。
编辑:
根据AKhill在下面的回答,我已经修改了我的GLSurfaceView以接受一个ArrayList,如下所示。但是,MarkerCustom类需要在构造函数中从该列表中创建每个类,以便在GLSurfaceView的onSurfaceCreate和onDrawFrame方法中可以访问它。
请参见以下类中的这些方法、for循环和注释:
public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback, Renderer {

    private Context context;

    ArrayList <MarkerCustom> locationTags;

    private PhoneOrientation phoneOri;


    public GLLayer(Context context, int orientation, ArrayList<MarkerCustom> custMarkers) {
        super(context);
        locationTags = custMarkers;

        this.context = context;

        phoneOri=new PhoneOrientation(context); // sensor manager and interpreter

         for(int i =0; i<locationTags.size(); i++){

            //Need to create a an instance of each MarkerCustom
            // something like MarkerCustom locationTags[i]; = new MarkerCustom (its params);
        }
        this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        this.getHolder().setFormat(PixelFormat.TRANSLUCENT);

        this.setRenderer(this);
        phoneOri.start(context, orientation);

    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glEnable(GL10.GL_TEXTURE_2D);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glLoadIdentity();

        GLU.gluLookAt(gl,0, 0, 0, 0, 0, 0, 0, 0, 0);

        float floatMat[]=phoneOri.getMatrix();

        gl.glMatrixMode(GL10.GL_MODELVIEW);


        gl.glLoadMatrixf(floatMat, 0);


        for(int i=0;i<loacationTags.size();i++){
            gl.glPushMatrix();

            //locationTags[i].draw(gl);
            gl.glLoadMatrixf(floatMat,0);
        }


         gl.glPushMatrix();

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
      if(height == 0) {                    
        height = 1;                         
        }
        float ratio = (float) width / height;
        gl.glViewport(0, 0, width, height);     
        gl.glMatrixMode(GL10.GL_PROJECTION);    
        gl.glLoadIdentity();                    

        GLU.gluPerspective(gl, 35.0f, (float)width / (float)height, 5.0f, 200.0f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);     
        gl.glLoadIdentity();                    

        GLU.gluLookAt(gl, 0, 1.0f, 5.0f, 0, 0, 0, 0, 1.0f, 0);


    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {

        for(int i=0;i<locationTags.size();i++){
            //call each MarkerCustom's loadGLTexture Method
                //locationTags[i].loadGLTexture(gl, this.context);
        }


        gl.glEnable(GL10.GL_TEXTURE_2D);            
        gl.glShadeModel(GL10.GL_SMOOTH);            
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    
        gl.glClearDepthf(1.0f);                     
        gl.glEnable(GL10.GL_DEPTH_TEST);           
        gl.glDepthFunc(GL10.GL_LEQUAL);             


        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_SMOOTH);

    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {


    }


}

仅供参考,这是我的MarkerCustom


public class MarkerCustom {

    public float xPos;
    public float yPos;
    public float zPos;
    public float yaw;
    public float pitch;
    public Bitmap tagImage;

    private FloatBuffer vertexBuffer;   // buffer holding the vertices
    private float vertices[] = {
            0.0f, -10.0f, -10.0f,        // V1 - bottom left
            0.0f, -10.0f, 10.0f,        // V2 - top left
            0.0f, 10.0f, -10.0f,        // V3 - bottom right
            0.0f, 10.0f, 10.0f         // V4 - top right

    };

    private FloatBuffer textureBuffer;  // buffer holding the texture coordinates
    private float texture[] = {
            // Mapping coordinates for the vertices
            0.0f, 1.0f,     // top left     (V2)
            0.0f, 0.0f,     // bottom left  (V1)
            1.0f, 1.0f,     // top right    (V4)
            1.0f, 0.0f      // bottom right (V3)

    };


    public MarkerCustom(float x, float y, float z, float yawAngle, float pitchAngle, Bitmap bitmap) {

        xPos = x;
        yPos = y;
        zPos = z;
        yaw = yawAngle;
        pitch = pitchAngle;
        tagImage = bitmap;

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuffer.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

        byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        textureBuffer = byteBuffer.asFloatBuffer();
        textureBuffer.put(texture);
        textureBuffer.position(0);

    }
    /** The texture pointer */
    private int[] textures = new int[1];

    public void loadGLTexture(GL10 gl, Context context) {
        // loading texture
        // Enable blending using premultiplied alpha.
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);



        // generate one texture pointer
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // create nearest filtered texture
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

        // Use Android GLUtils to specify a two-dimensional texture image from our bitmap
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, tagImage, 0);

        // Clean up
        tagImage.recycle();
    }
    public void draw(GL10 gl) {
        // bind the previously generated texture
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // Point to our buffers
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        // Set the face rotation
        gl.glFrontFace(GL10.GL_CW);

        // Point to our vertex buffer
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        // Draw the vertices as triangle strip
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

        //Disable the client state before leaving
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    }

}
3个回答

22

试试这个。

List<MarkerCustom> myList=new ArrayList<MarkerCustom>();
MarkerCustom entry1=new MarkerCustom(myInt, myString, myBitmap);
MarkerCustom entry2=new MarkerCustom(myInt, myString, myBitmap);

myList.add(entry1);
myList.add(entry2);

问题在于标记的数量不是预先确定的,它将是从数据库查询中得到的列表。这个代码可以工作吗? for(int i=0; i<results.size; i++){ MarkerCustom tempMarker = new MarkerCustom(results[i][0],results[i][1],results[i][2]); myList.add(tempMarker); } - erik
谢谢...对不起我的格式..我不知道如何在评论中创建代码块..只是为了明确,我可以说results[i][0]吗? - erik
结果是数组还是列表?因为results.size只适用于列表,对于数组来说应该使用results.length。 - Akhi
我还没有决定...基本上,我将在在线MySQL数据库中拥有一个表,我将通过服务器端脚本(可能是PHP)对其进行ping。这就是我在Java方面感到有些困惑的地方...在ActionScript中,数组是数组,没有固定大小...但在Java中,数组具有固定大小,对吗? - erik
我原以为这样会管用,但我在将ArrayList传递给我的GLSurfaceView后,实际上无法全局实例化MarkerCustom,希望您能看到我上面的编辑。我已经添加了我的GLSurface类和MarkerCustom类,希望您能更好地理解我想做什么。谢谢。 - erik
显示剩余4条评论

9

速记:

List<MarkerCustom> markerList = Arrays.asList(
    new MarkerCustom(1, "hello", bitMapData1),
    new MarkerCustom(66, "johnHandy", bitMapData2))
};

值得注意的是,此列表将具有固定大小。 - assylias
我认为这条路是正确的,但如果我要从在线获取数据列表,然后执行类似以下操作,这是否可行: for(int i=0; i<results.size; i++){ MarkerCustom tempMarker = new MarkerCustom(results[i][0],results[i][1],results[i][2]); markerList.add(tempMarker); } - erik
由于此列表是固定的,因此添加方法不可用。因此,您必须使用Akhil Dev上面指示的方法。 - Reimeus

0
在这种情况下,我认为更容易创建一个名为Marker(例如)的类,该类具有三个属性。例如:
class Marker {

int var1;
String var2;
BitmapData var3; }

通过这种方式,您可以存储标记的ArrayList(ArrayList),并且可以访问您需要的所有信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接