0

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

3 Answers 3

1

Yes, you need to use generics to say what kind of object will be in your list:

ArrayList<MyObj> al = new ArrayList<MyObj>();

Otherwise the compiler doesn't know that al.get(3) is meant to return a MyObj rather than just anything. With this code, it will not only fix the get calls - it will stop you from accidentally adding objects of inappropriate types to the list.

Also, it's usually preferred to declare variables as an appropriate interface type, such as:

List<MyObj> al = new ArrayList<MyObj>();

(There are times where you really need it to be the concrete type, but generally prefer programming to interfaces.)

Generics is a massive topic, but the tutorial linked at the start of this answer should get you going. The Java Generics FAQ is a great resource for finding out more.

Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks for your answer. Does this mean then that it is not possible to achieve my goal without the generics <>? I guess I will have to read up on them, but if there is a simpler way in the mean time, then I would be interested to know !
@user2094585: Well you could cast, or use an array instead - but it would be best to learn generics. They're a core part of modern Java.
OK Jon, I will have a look at both. Thanks.
0

Change

    ArrayList al = new ArrayList();

to

    ArrayList<MyObj> al = new ArrayList<MyObj>();

What you have right now is an array list of Object rather than an array list of MyObj, and Object has no knowledge of MyObj.X.

Comments

0

Use ArrayList<MyObj> al = new ArrayList<MyObj>(); instead

1 Comment

use List<MyObj> al = new ArrayList<MyObj>(); instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.