-1
List<ArrayList<Planet>> NAME = new ArrayList<ArrayList<Planet>>(N);
List<List<Planet>> NAME = new ArrayList<ArrayList<Planet>>(N);
ArrayList<ArrayList<Planet>> NAME = new ArrayList<ArrayList<Planet>>(N);

I can't tell what's the difference of these three expressions. I realized later that maybe List is for declaration because its an interface while ArrayList could be used for initiation, as it is a class. I took the first syntax in my project and find

found : java.util.ArrayList>

required: java.util.ArrayList>

List<List<Planet>> Name = new ArrayList<ArrayList<Planet>>(N);

I have added at the header.

import java.util.ArrayList;
import java.util.List;

In another place, while I am adding Planet Object into the ArrayList:

for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (i == j) continue;
                NAME[i].add(planets[j]);
            }
        }

The compiler reports,

array required, but java.util.ArrayList> found

          planetsForNetForceSetting[i].add(planets[j]);

Could anybody tell me the reason for this?

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
tonyabracadabra
  • 319
  • 2
  • 10
  • The message is pretty clear array required, but java.util.ArrayList> found. To access members of list use get(i) method instead of [i] syntaх – vrudkovsk Jun 11 '15 at 13:40

3 Answers3

3

You are trying to retrieve data from given position of a List like an Array here: NAME[i].add(planets[j]);, what cannot be done.

To get data from an ArrayList you must use get(int) method. In thi way: NAME.get(i) instead NAME[i].

Then, your method must be

for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (i == j) continue;
            NAME.get(i).add(planets[j]);
        }
    }




About difference of three expressions, and why use interface to declare List<Object> list = new ArrayList<Object> instead of the class itself, look at this question.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

Out of your 3 expressions, in same order as you mentioned, 2nd one is the most generic one, then 1st and then 3rd.
Which means that when you use List<Planet> listObj then you can use listObj to point to any implementation of List interface. While when you do ArrayList<Planet> listObj then you can use listObj to point to only an object of type ArrayList
Whether 1 dimensional or N dimensional, this hold true.

A more generic type and preferred one would be use of List<Object> listObj, which means that you can use listObj to point to any implementation of List interface, ALSO you in the list you can put any object. When you use List<Planet> listObj then you can only put Planet objects.

For your compile problem, your code is not sufficient to figure out root cause because I cannot see whats planetsForNetForceSetting, but just make sure that you are putting right objects in right place. Like I explained above.

Hope this helps!!!

hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
1

First List<T> is a java interface parametrized by type T. ArrayList<T> is specific implementation of interface List<T>. So you may assign List<Integer> list = new ArrayList<Integer>(); This way variable list will have type List<Integer> which will hide specific implementation ArrayList<T>.

Then following lines:

List<ArrayList<Planet>> planetsList = new ArrayList<ArrayList<Planet>>();

creates variable planetsList of type List<ArrayList<Planet>> which is list of array lists. Where actual outer lists implementation is ArrayList<ArrayList<Planet>>.

List<List<Planet>> planetsList2 = new ArrayList<ArrayList<Planet>>();

Will not compile, since you cannot assign ArrayList<ArrayList<Planet>> to List<List<Planet>> Refer to this question to more deep explanation why Why can't you have a "List<List<String>>" in Java?

ArrayList<ArrayList<Planet>> planetsList3 = new ArrayList<ArrayList<Planet>>();

Will create variable planetsList3 with type ArrayList<ArrayList<Planet>>.

You need to import both java.util.List and java.util.ArrayListtypes since you are directly referencing them in your code.

And finally. You cannot assign item to list's index by array indexing syntax items[index]=item; since List<ItemType> items; is an interface not an array. These are totally different things in java.

Community
  • 1
  • 1
plastique
  • 1,164
  • 2
  • 9
  • 19