如何在Java中的Array列表中查找存储在对象中的值

时间:2016-12-14 17:44:42

标签: java arrays arraylist

我已经阅读了很多关于此的帖子,我尝试过的任何内容似乎都没有用。任何帮助将不胜感激。

我有2个课程,ColouredShapeShapeMatchingGame。 我正在尝试使用2个参数创建ColouredShape个对象,并将它们添加到ShapeMatchingGame类中的ArrayList。 我需要4种形状* 3的4种形状和3种颜色,所以阵列中有36个项目。

我可以看到36个对象正在循环中添加到数组中但是每当我尝试访问数组中对象的值时,我都没有在其索引处看到对象的预期值。 我想我想问的是我是否错误地访问了这些值,或者我在创建对象并将它们添加到数组时做错了什么?

public class ColouredShape {

static int shapeValue;
static int colourValue;
static String colour, shape;

public ColouredShape() // Default constructor
{
    this.shapeValue = 1;
    this.colourValue =1;
    this.colour ="";
    this.shape ="";
}

public ColouredShape (int shapeValue,int colourValue) // Constructor with 2 arguments
{
    this.shapeValue = shapeValue;
    this.colourValue =colourValue;
    this.colour = colour;
    this.shape =shape;
}


    public int getColour()
    {
        return colourValue; 
        }       

    public int getShape()
    {
        return shapeValue; 

        }

public class ShapeMatchingGame {

static int noShapes;
static ArrayList<ColouredShape> ColouredShapes = new ArrayList<ColouredShape>();
static int index;
static int shapeValue=1;
static int colourValue;

public static void main(String[] args)
{

    ObjectCreation();
}



    public static void ObjectCreation()
    { 
        do// runs loops 3 times to create 3 of every shape/colour combo
        {           
            do // loop to continue onto the next shape until 4 are created
            {   
                do // loop to create 3 of colours of same shape
                {       
                    colourValue++;
                    System.out.println("Shape value " +shapeValue + " colour value " +colourValue);
                    ColouredShape gameShapes = new ColouredShape(shapeValue,colourValue);
                    ColouredShapes.add(gameShapes);//creates an object of colourshapes and passes the current shapevalue + colourvalue as arguments then adds it to the arraylist
                    System.out.println ("Value of object at array index "+ index  + " shape value " + ColouredShapes.get(index).getShape()+" colour value " +ColouredShapes.get(index).getColour()+ "colour variable value = " + colourValue);                      
                    index++;
                    for (ColouredShape colouredShape : ColouredShapes) 
                    {
                        System.out.println(colouredShape.getClass().getName() + "/" +
                                colouredShape.shape + "/" +
                                colouredShape.colour);

                    }

                }while(colourValue < 3 );
                System.out.println ("Value of object at array index "+ "0"  + " shape value " + ColouredShapes.get(0).getShape()+" colour value " +ColouredShapes.get(0).getColour()+ "colour variable value = " + colourValue);
                colourValue=0;//reset colourValue to allow next iteration of the loop
                shapeValue++;//incrementing shapeValue to add colours to next shape 
                System.out.println ("Value of object at array index "+ "0"  + " shape value " + ColouredShapes.get(0).getShape()+" colour value " +ColouredShapes.get(0).getColour()+ "colour variable value = " + colourValue);
            }while(shapeValue < 5 );

            shapeValue=1; // resetting shapeValue to allow next iteration of the loop
            noShapes++;

        }while (noShapes<3);

   }

}

1 个答案:

答案 0 :(得分:0)

您的ColouredShape有几个问题:

  1. shapeValue和colourValue是静态的。这意味着他们在那里“全球化”。变量。与您创建的Colouredshapes数量无关,所有这些都具有相同的值
  2. 构造函数仅设置静态变量的值,并打印字符串文本的值。
  3. 将对象更改为:

    public class ColouredShape {
    
        int shapeValue;
        int colourValue;
    
        public ColouredShape() // Default constructor
        {
            this.shapeValue = 1;
            this.colourValue = 1;
        }
    
        public ColouredShape(int shapeValue, int colourValue) // Constructor with 2
                                                            // arguments
        {
            this.shapeValue = shapeValue;
            this.colourValue = colourValue;
        }
    
        public int getColour() {
            return colourValue;
        }
    
        public int getShape() {
            return shapeValue;
        }
    }
    

    ,结果应符合预期