-1

So, I have a class that creates an ArrayList of objects, but I have another superior class that is the one that operates with that ArrayList, so I understand I have to create a new object of that class. How can I do it? I have done it in a way it doesn't accept the ArrayList methods (add, size...)

Probably this is simple, but I just started Java. like this:

public class Objects {
    private List<objects> list = new ArrayList<>
}

-------------------------

public class List {
    private Objects list;

    public List() {
        list= new Objects;
    }

It looks like it is not like this, because I cant use add, get or size. I would appreciate some help. And if you are asking, I need the objects class. Thank you.

--------------------------------------- EDIT----------------------------------

I need a method to add an object, but it says add, size and get is not defined. i thought i can use de default ones

public void add (Object pea){
this.list.add(pea);
}

it says it doesnt work

Alessio
  • 3,404
  • 19
  • 35
  • 48
eisklat
  • 41
  • 6
  • Make "list" public. It is private now. You cannot access outside from class – kaan bobac Oct 15 '18 at 10:27
  • Look at https://stackoverflow.com/questions/3527264/how-to-create-a-pojo you can build a similar class with your list as member. – jschnasse Oct 15 '18 at 10:28
  • Still not working... it says method add is not defined i need to use a method to add an object in List into the Arraylist of objects... like, i thought i can use the normal Arraylist methods, why cant i? – eisklat Oct 15 '18 at 19:05

1 Answers1

0
public class Foo {

    public static void main(String... args) {
        // create instances of two classes
        One one = new One();
        Two two = new Two();

        // call required methods to create and use a list
        two.printArrayList(one.createNewArrayList());
    }

}

public class One {
    // create new array list
    public List<String> createNewArrayList() {
        return new ArrayList<>();
    }
}

public class Two {
    // print given array list to the standard output stream
    public void printArrayList(List<String> list) {
        list.forEach(System.out::println);
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • i need the list in a method to add an object i receive: public void add (Object pea){ this.list.add(pea); } but it says it doesnt work – eisklat Oct 15 '18 at 19:06