Java 对arrayList感到困惑

Java 对arrayList感到困惑,java,Java,当我向老师寻求帮助时,我得到了这行代码,但在最后一部分下面我得到了一条红线。有什么不对劲吗?错误消息:“表达式的类型必须是数组类型,但它已解析为ArrayList”我不理解,请帮助我理解 ArrayList<Point>[] touchPoints = new ArrayList<Point>()[2]; 看看这个 数组对象的组件类型不能是类型变量或参数化类型,除非它是(无界)通配符类型。可以声明元素类型为类型变量或参数化类型的数组类型,但不能声明数组对象 看看这个 数

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

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

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

看看这个

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


你混合了两件事:

  • 构造普通数组
  • 构造数组列表
构造数组 普通数组的级别非常低。没有方法,其长度在创建后是固定的

MyType[] anArray = new MyType[10]; 
构造数组列表 ArrayList只是一种集合类型的实现

Collection<MyItemType> aCollection = new ArrayList<MyItemType>();
Collection aCollection=newarraylist();
在你的情况下该怎么办? 您需要一个集合的普通数组(它的实现是ArrayList)。因此:

//创建数组,在以后需要更改实现时使用接口
集合[]接触点=(集合)新集合[2];
//使用ArrayList实现在该数组中创建每个集合
接触点[0]=新的ArrayList();
接触点[1]=新的ArrayList();
如何做得更好? 试着想想为什么需要一个普通数组:

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

根据您的用例进行编辑:

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

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用户输入{
公共用户输入(){
user1TouchPoints=newarraylist();
user2TouchPoints=newarraylist();
}
//添加访问器和所有
私人收藏用户1触摸点;
私人收藏用户2触摸点;
}

如果你计划有更多的玩家,只需使用地图

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;
}
class用户输入{
公共用户输入(){
usersTouchPoints=newhashmap();
}
公共集合getUserTouchPoints(整数用户ID){
返回usersTouchPoints.get(userId);
}
public void addUserTouchPoints(整数用户ID、集合输入){
集合点=usersTouchPoints.get(userId);
如果(点==null){
points=新的ArrayList();
userTouchPoints.put(userId,points);
}
points.addAll(输入);
}
//将用户ID(或索引)映射到其接触点
//如果您使用的是Android,请使用SparseArray而不是Map,这样效率更高
私有地图用户接触点;
}

您混合了两件事:

  • 构造普通数组
  • 构造数组列表
构造数组 普通数组的级别非常低。没有方法,其长度在创建后是固定的

MyType[] anArray = new MyType[10]; 
构造数组列表 ArrayList只是一种集合类型的实现

Collection<MyItemType> aCollection = new ArrayList<MyItemType>();
Collection aCollection=newarraylist();
在你的情况下该怎么办? 您需要一个集合的普通数组(它的实现是ArrayList)。因此:

//创建数组,在以后需要更改实现时使用接口
集合[]接触点=(集合)新集合[2];
//使用ArrayList实现在该数组中创建每个集合
接触点[0]=新的ArrayList();
接触点[1]=新的ArrayList();
如何做得更好? 试着想想为什么需要一个普通数组:

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

根据您的用例进行编辑:

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

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用户输入{
公共用户输入(){
user1TouchPoints=newarraylist();
user2TouchPoints=newarraylist();
}
//添加访问器和所有
私人收藏用户1触摸点;
私人收藏用户2触摸点;
}

如果你计划有更多的玩家,只需使用地图

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;
}
class用户输入{
公共用户输入(){
usersTouchPoints=newhashmap();
}
公共集合getUserTouchPoints(整数用户ID){
返回usersTouchPoints.get(userId);
}
public void addUserTouchPoints(整数用户ID、集合输入){
集合点=usersTouchPoints.get(userId);
如果(点==null){
points=新的ArrayList();
userTouchPoints.put(userId,points);
}
points.addAll(输入);
}
//将用户ID(或索引)映射到其接触点
//如果您使用的是Android,请使用SparseArray而不是Map,这样效率更高
私有地图用户接触点;
}

您已经创建了一个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"

    }
}
import java.util.ArrayList;
公共类ArraySandList{
公共静态void main(字符串[]args){
ArrayList[]接触点=新的ArrayList[2];
//增值
接触点[0]=新的ArrayList();
接触点[0]。添加(“第一个ArrayList中的一个字符串”);
接触点[0]。添加(“第一个ArrayList中的另一个字符串”);
接触点[1]=新的ArrayList();
触点[1]。添加(“第二个ArrayList中的一个字符串”);
触点[1]。添加(“第二个ArrayList中的另一个字符串”);
//接触点[2]。添加(“这将给出边界,数组仅包含两个列表”);
//读取值
System.out.println(接触点[0].get(0));//返回“第一个ArrayList中的一个字符串”
System.out.println(接触点[1].get(1));//返回“anoth”