I need to store some entity object + counter of each groups entities objects. How can I do this?
我需要存储每个组实体对象的一些实体对象+计数器。我怎样才能做到这一点?
3 个解决方案
#1
1
If the counter variable is not a part of the object itself, you can use Map<Object,Integer>
as other answers suggest. However, keep in mind that you can use any collection or list if the counter is a part of the object data. Then you will update the counter with setters. Alternatively, updating the counter within particular constructors of various classes may also be a preferred way.
如果计数器变量不是对象本身的一部分,则可以使用Map ,integer>
class Data
{
int counter = 0;
Data()
{
counter++;
}
}
#2
1
You can use a Map
您可以使用地图
Map<Object, Integer> map = new HashMap<Object, Integer>();
Here, Object
is your key and Integer
is your count .
在这里,对象是你的关键,整数是你的计数。
- Your keys should be unique in the HashMap.
- As it uses hashing, it will help in efficient retrieval of your objects while searching.
您的密钥在HashMap中应该是唯一的。
因为它使用散列,它将有助于在搜索时有效地检索对象。
#3
0
Any Map will do. Alternatively if you're not interested in learning about new classes simply use a matrix.
任何地图都可以。或者,如果您对学习新课程不感兴趣,只需使用矩阵即可。
int matrix[][]=new int[10][10];
int matrix [] [] = new int [10] [10];
#1
1
If the counter variable is not a part of the object itself, you can use Map<Object,Integer>
as other answers suggest. However, keep in mind that you can use any collection or list if the counter is a part of the object data. Then you will update the counter with setters. Alternatively, updating the counter within particular constructors of various classes may also be a preferred way.
如果计数器变量不是对象本身的一部分,则可以使用Map ,integer>
class Data
{
int counter = 0;
Data()
{
counter++;
}
}
#2
1
You can use a Map
您可以使用地图
Map<Object, Integer> map = new HashMap<Object, Integer>();
Here, Object
is your key and Integer
is your count .
在这里,对象是你的关键,整数是你的计数。
- Your keys should be unique in the HashMap.
- As it uses hashing, it will help in efficient retrieval of your objects while searching.
您的密钥在HashMap中应该是唯一的。
因为它使用散列,它将有助于在搜索时有效地检索对象。
#3
0
Any Map will do. Alternatively if you're not interested in learning about new classes simply use a matrix.
任何地图都可以。或者,如果您对学习新课程不感兴趣,只需使用矩阵即可。
int matrix[][]=new int[10][10];
int matrix [] [] = new int [10] [10];