-1

Probably I'm being blind or something, but I can't seem to figure out how to do the following:

I have a Class called Shopping with a constructor like this:

Shopping shopping = new Shopping(String name, ArrayList <Shop> shops)

The class Shop is superclass for 4 types of sub-shops, and its constructor is something like this:

Shop shop = new Shop(String name, double area, double income)

My question is: when creating a Shopping instance I don't know what to write on the Array list part of the constructor, like I can create a

Shopping s1 = new Shopping("ABCDE", ?????? <- but don't know what to write here!!!)

Any help?

David Conrad
  • 15,432
  • 2
  • 42
  • 54
Luís Lira
  • 33
  • 2
  • Depends how you're creating your shop list. You could create it first then pass it to the constructor, or if you're creating it later make a constructor that only receives `string name` and sets `shops` to null inside the constructor (or creates an empty list), then fill the list later – Janilson Apr 16 '18 at 22:29
  • @Zabuza Please don't answer questions in the comments. Comments are for gathering more information to improve or clarify the question. – David Conrad Apr 16 '18 at 22:42
  • 1
    @DavidConrad Sometimes I don't have the time to write an answer so detailed that I would want to paste it as answer. Because an answer should fully solve the issue, clarifying all confusion. In that case it's good to just paste the short high level description as comment. But in this case I realized that I want to extend and write an answer, so I just removed the comment after writing the answer. – Zabuzard Apr 16 '18 at 22:47

4 Answers4

1

The short answer is that you need to pass it an instance of the type you've declared. If that type is ArrayList<Shop> then you need to pass it one of those. You can create one of those with `new ArrayList()', for example, or you might have some code that constructs such a list, or even that already has one because it was doing something else with a list of shops.

However, you've backed yourself into a corner by saying it has to be an ArrayList. You should only declare it to take a List, and only if the order of the shops is important. If the order isn't important, then just declare a Collection<Shop>. Always declare your arguments to be as non-specific as you can. If it's a Collection, then someone who happens to have a Set<Shop> in his hand can pass it to you, and it will work fine.

Zag
  • 638
  • 4
  • 8
1

Explanation

You create a new instance of the object using

ArrayList<Shop> shops = new ArrayList<>();

and then you add your stuff

shops.add(firstShop);
shops.add(secondShop);

and after that you just pass the variable to the method

Shopping s1 = new Shopping("ABCDE", shops);

Or if you want to pass an empty list you can simply do it in one line

Shopping s1 = new Shopping("ABCDE", new ArrayList<>());

Other lists

Is there a particular reason you need to limit the method to ArrayLists? Wouldn't List be enough too? If you adjust that you can pass other lists too and with Java 9 there is a simple way to create lists quickly:

List<Shop> shops = List.of(firstShop, secondShop);
Shopping s1 = new Shopping("ABCDE", shops);

Or even more compact:

Shopping s1 = new Shopping("ABCDE", List.of(firstShop, secondShop));
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
1

Two possibilities. If you've previously created some shops:

Shop subShop = new Shop("Subway", 2000, 4000);
Shop bookShop = new Shop("B&N", 8000, 9000);

And a list:

ArrayList<Shop> shops = new ArrayList<>();
shops.add(subShop);
shops.add(bookShop);

Then you can create it like this:

Shopping shopping = new Shopping("Plaza", shops);

However, I would change Shopping to take a List (or even a Collection, as Zag points out), rather than an ArrayList in which case you can do this:

Shopping shopping = new Shopping("Plaza", List.of(subShop, bookShop));

You can even write it all in one line, if you put the calls to the Shop constructors inside the call to List::of.

David Conrad
  • 15,432
  • 2
  • 42
  • 54
1
Shopping shopping = new Shopping(name, new ArrayList<>());

or if you want a list that already has some items in it:

List<Shop> shops = new ArrayList<>();
shops.add(new Shop(...));
shops.add(new Shop(...));
Shopping shopping = new Shopping(name, shops);

You can also do that in one line, if you want:

Shopping shopping = new Shopping(name, Arrays.asList(new Shop(...), new Shop(...)));
TwiN
  • 3,554
  • 1
  • 20
  • 31
  • 2
    `new List<>()` isn't valid Java. `List` is an interface and can't be instantiated. – David Conrad Apr 16 '18 at 22:42
  • @DavidConrad whoops, fixed it. My bad. – TwiN Apr 16 '18 at 22:44
  • I should've let you make the post for me. Multitasking isn't doing too well for me right now ;~; Thanks again @DavidConrad – TwiN Apr 16 '18 at 22:48
  • By the way, the last works only if the method takes general `List`s too. Because the list returned by `Arrays.asList` is not `ArrayList` (it has the same name though, but it actually is a different class implemented in the `Arrays` class). – Zabuzard Apr 16 '18 at 22:48
  • 1
    @Zabuza `new ArrayList<>(Arrays.asList("a", "b"))` it can still be used like so, assuming that he really does use ArrayList instead of List as parameter – TwiN Apr 16 '18 at 22:52