0

当我向老师寻求帮助时,我得到了这行代码,但我在最后一部分下方出现了一条红线。有什么问题?错误消息:“表达式的类型必须是数组类型,但它解析为 ArrayList”我不明白,请帮助我理解。

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

我想要两个列表来保存积分。我想我把每个列表都称为touchPoints[0];touchPoints[1];!?

编辑:

我想我可以保持简单,只使用两个这样的不同列表!?:

points1 = new ArrayList<Point>();
points2 = new ArrayList<Point>();
4

3 回答 3

2

您已经创建了一个 ArrayList 数组。这个演示展示了它们是如何一起使用的

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"

    }
}
于 2013-06-15T08:42:44.517 回答
1

看看这个问题

数组对象的组件类型可能不是类型变量或参数化类型,除非它是(无界)通配符类型。您可以声明元素类型为类型变量或参数化类型的数组类型,但不能声明数组对象。

于 2013-06-15T08:39:53.090 回答
1

你混合了两件事:

  • 构造一个普通数组
  • 构造一个 ArrayList

构造数组

普通数组是非常低级的。没有方法,创建后它的长度是固定的。

MyType[] anArray = new MyType[10]; 

构造一个 ArrayList

ArrayList 只是一种 Collection 的实现

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

在你的情况下该怎么办?

你想要一个简单的集合数组(实现是 ArrayList)。所以:

// 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>();

如何做得更好?

试着想想为什么你需要一个普通的数组:

  • 如果它只有 2 个元素,并且总是固定的,只需创建两个成员变量。
  • 如果数量可以变化,只需创建一个集合集合 (Collection>)

根据您的用例进行编辑:

只需创建一个类来保存您的用户输入:

class UserInput {

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

  // Add accessors and all

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

如果您打算拥有更多玩家,只需使用地图

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;
}
于 2013-06-15T08:41:29.943 回答