im having a bit of trouble using the object i add to my arraylist. i used this as my reference:
http://www.java2s.com/Tutorial/CSharp/0220__Data-Structure/ArrayListandyourownobjectAddobjectstoArrayList.htm

import java.util.ArrayList;

public class TestArrayList
{
    public static void main(String[] args)
    {
        Object item1 = new ObjectMaker();
        Object item2 = new ObjectMaker();
        Object item3 = new ObjectMaker();

        ArrayList<Object> arrayList = new ArrayList<Object>();
        arrayList.add(item1);
        arrayList.add(item2);
        arrayList.add(item3);
        System.out.println(arrayList);
        //you made three objs item 1 2 3, added them to arrayList 0 1 2
        //call method on each obj
        for (int i = 0; i <= arrayList.size(); i++)
        {
            arrayList.get(i).info();
        }
        
    }
}
public class ObjectMaker
{
    private static int count = 0;
    String name;
    int year;
    ObjectMaker()
    {
        count++;
        System.out.println("Making Object #" + count);
    }

    public void info()
    {
        System.out.println("You have used the obj in array list correctly");
    }

}

Dani AI

Generated

Two separate issues are tripping you up here: typing and iteration bounds.

  • If you store elements as Object, the compiler only lets you call methods defined on Object. Either parameterize the list and variables with your class, or you must cast before calling info(). Prefer generics to avoid casts:
    
    import java.util.ArrayList;
    import java.util.List;

List<ObjectMaker> items = new ArrayList<>();
items.add(new ObjectMaker());
items.add(new ObjectMaker());
items.add(new ObjectMaker());

// safe: compiler knows each element is an ObjectMaker
items.forEach(ObjectMaker::info);

If you insist on `ArrayList<Object>`, you must cast (risking ClassCastException): `((ObjectMaker) arrayList.get(i)).info();`. See [Generics tutorial](https://docs.oracle.com/javase/tutorial/java/generics/index.html) and [List<E> Javadoc](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html).

- Your loop condition should use `<`, not `<=`. Using `<= arrayList.size()` will throw IndexOutOfBoundsException on the last iteration:
```java
for (int i = 0; i < items.size(); i++) {
    items.get(i).info();
}

One more tip: System.out.println(arrayList); calls toString() on each element. Override it to get meaningful output:

@Override
public String toString() {
    return "ObjectMaker{name=" + name + ", year=" + year + "}";
}

Oracle’s collections overview explains why programming to interfaces (List instead of ArrayList) is recommended: Collections Framework Overview.

Recommended Answers

All 6 Replies

Well you made mistake on line 7-9 where you initialize Object based on ObjectMaker. Just change Object item1 to ObjectMaker item1 .

Well you made mistake on line 7-9 where you initialize Object based on ObjectMaker. Just change Object item1 to ObjectMaker item1 .

didnt work. on line 20 in still getting the error:

cannot find symbol
symbol: method info()
location: class java.lang.Object
Member Avatar for Member #887084

Use this instead:

ObjectMaker item1 = new ObjectMaker();
        ObjectMaker item2 = new ObjectMaker();
        ObjectMaker item3 = new ObjectMaker();

        ArrayList<ObjectMaker> arrayList = new ArrayList<ObjectMaker>();

The issue you are having is with calling Object.info(). This is not a method of the Object class (http://download.oracle.com/javase/1.3/docs/api/java/lang/Object.html). Essentially, all objects extend the Object class. This means, all classes inherit the methods of the Object class, but not all Objects inherit your .info() method. You should avoid using Object generically, when possible...

Object s = "pi";
	Object n = 3.1415;
	Object p = Math.PI;

All 3 objects; very different outcomes. Hope this helps!

commented: thanx +0

didnt work. on line 20 in still getting the error:

cannot find symbol
symbol: method info()
location: class java.lang.Object

As ztini already provided you been supposed to change all of Object instances to ObjectMaker...

This is what I wanted of you

import java.util.ArrayList;

public class TestArrayList
{
    public static void main(String[] args)
    {
        ObjectMaker item1 = new ObjectMaker();
        ObjectMaker item2 = new ObjectMaker();
        ObjectMaker item3 = new ObjectMaker();

        ArrayList<ObjectMaker> arrayList = new ArrayList<ObjectMaker>();
        arrayList.add(item1);
        arrayList.add(item2);
        arrayList.add(item3);
        System.out.println(arrayList);
        //you made three objs item 1 2 3, added them to arrayList 0 1 2
        //call method on each obj
        for (ObjectMaker om : arrayList)
        {
            om.info();
        }

    }
}

<original text deleted>

Do not hijack old threads. Start your own thread to ask a question

by learning the language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.