I want to be able to make an array list of objects, and then be able to edit and read back some simple properties of each object. In this case, a String (colour) and integer (X). I can't seem to make the simple code below work. Note that I am aware people sometimes use the <> notation with array lists, but I have read that it should be possible without, and at the moment I am very new to Java and wish to keep things as simple as possible.
At the moment I get an error on the line which is commented out (cannot find symbol X)
import java.util.ArrayList;
public class ArrayList_of_Objects {
public static void main(String[] args)
{
ArrayList al = new ArrayList();
for(int i =0;i<5;i++)
{
MyObj ob1 = new MyObj();
ob1.X = i + 5;
al.add(ob1);
//System.out.println("X: "+al.get(i).X);
}
for(int j=0;j<5;j++)
{System.out.println("X: "+al.get(j).X);}
al.get(3).X=4;
al.get(3).colour="orange";
System.out.println(al.get(3).X);
System.out.println(al.get(3).colour);
}
}
class MyObj
{
int X;
String colour;
}