0

I got this line of code when I asked the teacher for some help, but I get a redline below the last part. What could be wrong? The error message: "The type of the expression must be an array type but it resolved to ArrayList" I don't understand that, please help me to understand.

ArrayList<Point>[] touchPoints = new ArrayList<Point>()[2];

I want to have two lists to save Points. I guess I call each list like touchPoints[0]; and touchPoints[1]; !?

EDIT:

I guess I can keep it simple and just use two different List like this!?:

points1 = new ArrayList<Point>();
points2 = new ArrayList<Point>();
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159
  • @MarounMaroun Aha, like get.touchPoints[0] ? – 3D-kreativ Jun 15 '13 at 08:34
  • I don't think you want to create an array of arraylists at all (unless you have very ... strange requirements, there's barely ever a reason to mix arrays and collections). You simply want `ArrayList touchPoints = new ArrayList();` and access the elements with `touchPoints.get(0)` ... – Joachim Sauer Jun 15 '13 at 08:41
  • @JoachimSauer I need to handle touch inputs from two different users, that's why. Now I can only handle one users touch input with a list like this: points = new ArrayList(); – 3D-kreativ Jun 15 '13 at 08:43
  • 2
    And `touchPoints` is the *only* thing you want to handle for two users? If not, you should have a class that encapsulates information about the user that *contains* a `touchPoints` list and have an array (or list) of objects of *that* class. – Joachim Sauer Jun 15 '13 at 08:45
  • It is probably a duplicate question given and answered here 1. http://stackoverflow.com/questions/8483800/creating-an-array-of-generic-collections 2. http://stackoverflow.com/questions/470198/java-generics-and-array-initialization – Vaibhav Raj Jun 15 '13 at 08:52

3 Answers3

2

You have created an array of ArrayLists. This demo shows how they are used together

import java.util.ArrayList;

public class ArraysAndLists {
    public static void main(String[] args) {
        ArrayList<String>[] touchPoints = new ArrayList[2];

        // Adding values
        touchPoints[0] = new ArrayList<String>();
        touchPoints[0].add("one string in the first ArrayList");
        touchPoints[0].add("another string in the first ArrayList");

        touchPoints[1] = new ArrayList<String>();
        touchPoints[1].add("one string in the second ArrayList");
        touchPoints[1].add("another string in the second ArrayList");

        // touchPoints[2].add("This will give out of bounds, the array only holds two lists");

        // Reading values
        System.out.println(touchPoints[0].get(0)); // returns "one string in the first ArrayList"
        System.out.println(touchPoints[1].get(1)); // returns "another string in the second ArrayList"

    }
}
Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
  • Hmmm, thanks for the code example! Can I handle touch inputs from two users with this code? I was thinking to use event.getPointerCount() as an id of the users and for each list of points!? – 3D-kreativ Jun 15 '13 at 08:47
1

check out this Question

The component type of an array object may not be a type variable or a parameterized type, unless it is an (unbounded) wildcard type.You can declare array types whose element type is a type variable or a parameterized type, but not array objects.

Community
  • 1
  • 1
Mahmoud Ibrahim
  • 1,703
  • 15
  • 15
1

You are mixing two things:

  • Constructing a plain array
  • Constructing an ArrayList

Constructing an array

A plain array is very low level. Does not have methods, and its length is fixed after you create it.

MyType[] anArray = new MyType[10]; 

Constructing an ArrayList

ArrayList is just an implementation of a type of Collection

Collection<MyItemType> aCollection = new ArrayList<MyItemType>();

What to do in your case?

You want a plain array of collections (which implementation is ArrayList). So:

// Create the array, use the interface in case you need to change the implementation later on
Collection<Point>[] touchPoints = (Collection<Point>) new Collection[2];

// Create each collection within that array, using the ArrayList implementation
touchPoints[0] = new ArrayList<Point>();
touchPoints[1] = new ArrayList<Point>();

How to do it better?

Try to think about why you need a plain array:

  • if it's just 2 elements, and always fixed, simply create two member variables.
  • if number can vary, just create a Collection of Collections (Collection>)

Edit given your use case:

Just create a class to hold your user input:

class UserInput {

  public UserInput() {
    user1TouchPoints = new ArrayList<Point>();
    user2TouchPoints = new ArrayList<Point>();
  }

  // Add accessors and all

  private Collection<Point> user1TouchPoints;
  private Collection<Point> user2TouchPoints;
}

If you plan to have more players, simply use a map

class UserInput {

  public UserInput() {
    usersTouchPoints = new HashMap<Integer, Collection<Point>>();
  }

  public Collection<Point> getUserTouchPoints(Integer userId) {
    return usersTouchPoints.get(userId);
  }

  public void addUserTouchPoints(Integer userId, Collection<Point> input) {
    Collection<Point> points = usersTouchPoints.get(userId);
    if (points==null) {
      points = new ArrayList<Point>();
      userTouchPoints.put(userId, points);
    }
    points.addAll(input);
  }

  // Maps a user ID (or index) to its touch points
  // If you are using Android, use SparseArray instead of Map, this is more efficient
  private Map<Integer, Collection<Point>> usersTouchPoints;
}
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • 1
    No. He just asked how to create an Array of 2 elements, the 2 elements beeing ArrayList ... – Toilal Jun 15 '13 at 08:43
  • 1
    His question shows he knows little about that topic, so contrary to your very short answer, giving more info about it never hurts. That's how StackOverflow makes you learn things and not just get answers. – Vincent Mimoun-Prat Jun 15 '13 at 08:49