如何在类内创建一个ArrayList以及为什么要这样做?

huangapple go评论81阅读模式
英文:

How to make an ArrayList inside a class and why like that?

问题

我正在创建一个简单的游戏,在游戏中我有一个User类,用来存储用户数据。我想要在一个ArrayList中存储特定类型的数据,并在游戏中添加数据到其中。下面是我以为应该如何编写代码的方式,但显然这样是不正确的。为什么不正确?如何正确地做以及为什么要这样做?我在互联网上搜索过,但没有找到答案。

public class User {
    List positions;
}
User x = new User();
x.positions = new ArrayList();

x.positions.add(1);
x.positions.add(2);
英文:

I creating a simple game where I have a User class where I store user data. There is a specific kind of data that I want to store in a ArrayList and where I want to add things to during the game. Below you can see how I thought I had to write it, but apparently it is not correct like that. Why not? How do I do it correctly and why like that? I searched on the internet but couldn't find it.

public Class User {
    List positions;
}
User x = new User;
x.positions = new ArrayList;

x.positions.add(1);
x.positions.add(2);

答案1

得分: 2

首先,您应该将位置设置为私有的。
其次,您需要创建适当的公共getter和setter方法来访问这些位置。

参考下面的示例代码:
类 User(省略了包名):

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

public class User {
    private List<Integer> positions = new ArrayList<>();

    public List<Integer> getPositions() {
        return positions;
    }

    public void setPositions(List<Integer> positions) {
        this.positions = positions;
    }
}

类 Main(省略了包名):

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

public class MainApp {
    public static void main(String[] args) {
        User user = new User();
        user.getPositions().add(1);
        user.getPositions().add(2);

        // 另一种方式
        List<Integer> positions = user.getPositions();
        positions.add(3);

        // 另一种方式
        List<Integer> myNewList = new ArrayList<>();
        myNewList.add(4);
        myNewList.add(5);
        myNewList.add(6);
        // 以下将替换列表123为456
        user.setPositions(myNewList);
    }
}
英文:

First, you should make positions private.
Second, you need to make the appropriate public getter and setter methods of the positions.

See some sample below:
Class User (package is omitted):

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

public class User {
    private List&lt;Integer&gt; positions = new ArrayList&lt;&gt;();

    public List&lt;Integer&gt; getPositions() {
        return positions;
    }

    public void setPositions(List&lt;Integer&gt; positions) {
        this.positions = positions;
    }
}

Class Main (package is omitted):

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

public class MainApp {
    public static void main(String[] args) {
        User user = new User();
        user.getPositions().add(1);
        user.getPositions().add(2);

        // another way
        List&lt;Integer&gt; positions = user.getPositions();
        positions.add(3);

        // another way
        List&lt;Integer&gt; myNewList = new ArrayList&lt;&gt;();
        myNewList.add(4);
        myNewList.add(5);
        myNewList.add(6);
        // below will replace list 123 with 456
        user.setPositions(myNewList);
    }
}

答案2

得分: 1

我会将List更改为ArrayList<T>,其中T是对象的类型,因此它有存储什么内容的约束。此外,在您的类中,您希望设置私有变量,并使用设置器和获取器来访问这些变量。

我还包括了一个初始化块,该块构造了ArrayList<T>,并且在每次创建新的User对象时都会调用此初始化块,即使在类中有多个构造函数也是如此。如果不构造ArrayList<T>,它将为null。

public class User {
    private ArrayList<T> positions;

    public ArrayList<T> getPositions() {
        return this.positions;
    }

    public void setPositions(ArrayList<T> positions) {
        this.positions = positions;
    }

    // 初始化块
    {
        this.positions = new ArrayList<>();
    }
}

有了这个,您可以调用x.getPositions().add()来添加位置、查看位置、删除位置等等。

英文:

I would change List to ArrayList&lt;T&gt;, where T is a type of object, so it has a constraint of what can be stored in it. Also, in your class, you want to set it up so you have private variables, with setters and getters to access the variables.

I also included an initialzer block that constructs the ArrayList<T>, and this initializer block gets called on every creation of a new User object, even if you have multiple constructors in the class. Without constructing the ArrayList<T>, it would be null.

public class User {
    private ArrayList&lt;T&gt; positions;

    public ArrayList&lt;T&gt; getPositions() {
        return this.positions;
    }

    public void setPosition(ArrayList&lt;T&gt; position) {
        this.position = position;
    }

    // Initializer block
    {
        this.position = new ArrayList&lt;&gt;();
    }
}

With this, you can then call x.getPosition.add() to add positions, view positions, remove positions etc.

答案3

得分: 0

  1. 用正确的构造函数实例化你的 ArrayList(目前,你的代码不会编译,因为你没有调用构造函数。应该是 new ArrayList&lt;T&gt;(););

  2. 为你的 List 定义更具体的粒度,并声明它带有泛型类型参数,比如 List&lt;TypeYouWantToStore&gt;。否则,你的列表是 原始类型,不会对其中存储的内容有任何约束;

  3. 为你的 List 字段定义 访问器(getter)和 修改器(setter),而不是直接将其作为公共字段访问。然后你可以这样使用:

    x.getPositions().add(1);
    
  4. 我建议从 User 类的构造函数中初始化你的 List 字段。

英文:

Few things:

  1. Instantiate your ArrayList with a correct constructor (at the moment, your code won't compile, as you are not calling the constructor. It should be new ArrayList&lt;T&gt;(););

  2. Define more specific granularity for your List, and declare it with a Generic type parameter, like List&lt;TypeYouWantToStore&gt;. Otherwise, your list is of the raw type, and will not have any constraint on what you store in it;

  3. Define accessors (getters) and mutators (setters) for your List field, instead of accessing it directly as a public field. You'll then have:

    x.getPositions().add(1);
    
  4. I'd initialize your List field, from the constructor of User class.

huangapple
  • 本文由 发表于 2020年8月4日 19:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63246129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定