英文:
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<Integer> positions = new ArrayList<>();
public List<Integer> getPositions() {
return positions;
}
public void setPositions(List<Integer> 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<Integer> positions = user.getPositions();
positions.add(3);
// another way
List<Integer> myNewList = new ArrayList<>();
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<T>
, 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<T> positions;
public ArrayList<T> getPositions() {
return this.positions;
}
public void setPosition(ArrayList<T> position) {
this.position = position;
}
// Initializer block
{
this.position = new ArrayList<>();
}
}
With this, you can then call x.getPosition.add()
to add positions, view positions, remove positions etc.
答案3
得分: 0
-
用正确的构造函数实例化你的
ArrayList
(目前,你的代码不会编译,因为你没有调用构造函数。应该是new ArrayList<T>();
); -
为你的
List
定义更具体的粒度,并声明它带有泛型类型参数,比如List<TypeYouWantToStore>
。否则,你的列表是 原始类型,不会对其中存储的内容有任何约束; -
为你的
List
字段定义 访问器(getter)和 修改器(setter),而不是直接将其作为公共字段访问。然后你可以这样使用:x.getPositions().add(1);
-
我建议从
User
类的构造函数中初始化你的List
字段。
英文:
Few things:
-
Instantiate your
ArrayList
with a correct constructor (at the moment, your code won't compile, as you are not calling the constructor. It should benew ArrayList<T>();
); -
Define more specific granularity for your
List
, and declare it with a Generic type parameter, likeList<TypeYouWantToStore>
. Otherwise, your list is of the raw type, and will not have any constraint on what you store in it; -
Define accessors (getters) and mutators (setters) for your
List
field, instead of accessing it directly as apublic
field. You'll then have:x.getPositions().add(1);
-
I'd initialize your
List
field, from the constructor ofUser
class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论