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

}

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 ztini

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.