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

如何在Java中查找数组列表中对象中存储的值,java,arrays,arraylist,Java,Arrays,Arraylist,我读过很多关于这方面的帖子,但我尝试过的似乎都不管用。任何帮助都将不胜感激 我有两个类,coloredshape和shapemachinggame。 我试图使用两个参数创建coloredshape对象,并将它们添加到ShapeMatchingGame类中的ArrayList中。 我需要4个形状和3种颜色的4个形状*3,所以阵列中有36个项目 我可以看到36个对象添加到循环中的数组中,但是每当我尝试访问数组中对象的值时,我都看不到对象在其索引处的预期值。 我想我想问的是,我是否访问了错误的值,或者

我读过很多关于这方面的帖子,但我尝试过的似乎都不管用。任何帮助都将不胜感激

我有两个类,
coloredshape
shapemachinggame
。 我试图使用两个参数创建
coloredshape
对象,并将它们添加到
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);

   }

}
公共类彩色形状{
静态int-shapeValue;
静态int值;
静态线颜色、形状;
public coloredShape()//默认构造函数
{
this.shapeValue=1;
该值为1;
这个颜色=”;
此.shape=“”;
}
public-coloredShape(int-shapeValue,int-colorValue)//具有2个参数的构造函数
{
this.shapeValue=shapeValue;
this.colorValue=colorValue;
这个颜色=颜色;
这个形状=形状;
}
公共int getcolor()
{
返回颜色值;
}       
公共int getShape()
{
返回shapeValue;
}
公共类shapeMachinggame{
静态int-noShapes;
静态ArrayList coloredShapes=新ArrayList();
静态整数指数;
静态int shapeValue=1;
静态int值;
公共静态void main(字符串[]args)
{
ObjectCreation();
}
公共静态void ObjectCreation()
{ 
执行//运行循环3次,以创建每个形状/颜色组合的3个
{           
执行//循环以继续下一个形状,直到创建了4个
{   
循环创建3种相同形状的颜色
{       
颜色值++;
System.out.println(“形状值”+形状值+“颜色值”+颜色值);
coloredShape gameShapes=新的coloredShape(shapeValue、colorValue);
coloredShapes.add(gameShapes);//创建一个colorShapes对象,并将当前shapevalue+colorValue作为参数传递,然后将其添加到arraylist中
System.out.println(“数组索引处的对象值”+索引+“形状值”+彩色形状.get(index).getShape()+“颜色值”+彩色形状.get(index).getcolor()+“颜色变量值=“+彩色值”);
索引++;
用于(彩色形状彩色形状:彩色形状)
{
System.out.println(coloredShape.getClass().getName()+“/”+
彩色shape.shape+“/”+
彩色形状。彩色);
}
}而(色度值<3);
System.out.println(“数组索引“+”0“+”形状值“+彩色形状.get(0).getShape()+”颜色值“+彩色形状.get(0).GetColor()+”颜色变量值=“+彩色值”);
colorValue=0;//重置colorValue以允许循环的下一次迭代
shapeValue++;//递增shapeValue以向下一个形状添加颜色
System.out.println(“数组索引“+”0“+”形状值“+彩色形状.get(0).getShape()+”颜色值“+彩色形状.get(0).GetColor()+”颜色变量值=“+彩色值”);
}而(shapeValue<5);
shapeValue=1;//重置shapeValue以允许循环的下一次迭代
noShapes++;

}而(无形状您的彩色形状有几个问题:

  • shapeValue和ColorValue是静态的。这意味着它们是“全局”变量。无论创建多少个彩色形状,它们都将具有相同的值
  • 构造函数仅设置静态变量的值,并且打印字符串文本的值
  • 将对象更改为如下所示:

    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;
        }
    }
    

    结果应该和预期的一样

    为什么你有
    color
    colorValue
    ?看起来你的构造函数只使用了1。你的输出和你的预期输出可能是重复的?4 castle,color是用于其他东西的是colorValue类,我在这里没有显示,它基本上用于存储字符串到say colorValue等于什么。我没有在我的另一个类中使用它。公共静态字符串ShapeColour()//用于将形状作为字符串返回的方法{colorValue=randomColor();if(colorValue==1)color=Red;if(colorValue==2)color=Green;if(colorValue==3)color=Blue;return shape;}感谢您的回复,我在colorShape类中还有一些使用这些变量的代码,如果它们没有设置为static,它会标记一个错误。解决这个问题的最佳方法是什么?public static String ShapeShape()//将形状颜色返回为字符串{shapeValue=RandomShape();if(shapeValue==1)shape=Square;if(shapeValue==2)shape=Rectangle;if(shapeValue==3)shape=Triangle;if(shapeValue==4)shape=Circle;返回shape;}此方法取决于实例数据(对于您创建的每个对象,应该有不同的返回值),因此它不能是静态的。删除静态修改器,它应该可以工作