提问人:Martin Ma 提问时间:7/25/2023 最后编辑:Hovercraft Full Of EelsMartin Ma 更新时间:7/25/2023 访问量:53
一种在容器中对变量进行分组以进行集体操作并保持个人可访问性的方法
A way to group variables in a container for collective actions and maintain individual accessibility
问:
我有多个变量:
int a = 0;
int b = 1;
int c = 2;
//...
int z = 25;
有没有一种有效的方法可以对所有这些执行操作,或者有选择地执行操作?同时,每个变量都可以单独访问吗?像下面这样,只是一个想象:
Collection col = new Collection(a...z); // group all variables
for (int alphabet : col) { // change value of all variables
alphabet += 1;
}
System.out.print(a.toString()); // "1" // individual variable still accessible
System.out.print(z.toString()); // "26"
for (int alphabet : col) { // reassign all variables
int newAlphabet = alphabet * -1;
alphabet = newAlphabet;
}
System.out.print(a.toString()); // "-1" // individual variable still accessible
System.out.print(z.toString()); // "-26"
col.getIndex(1).set(99); // modify variables selectively
System.out.print(b.toString()); // "99" // individual variable still accessible
c = 0;
System.out.print(
col.getIndex(2).toString()); // "0" // value in collection updates with value in variable
使用 Array / ArrayList 来执行上述所有操作是可行的,但同时我仍然希望保留变量名称。
答:
0赞
Oleg Cherednik
7/25/2023
#1
您可以使用:Map
Map<Character, Integer> map = new HashMap<>();
for (char ch = 'a'; ch <= 'z'; ch++)
map.put(ch, ch - 'a');
map.keySet().forEach(key -> map.merge(key, 1, Integer::sum)); // change value of all variables
System.out.println(map.get('a')); // "1" // individual variable still accessible
System.out.println(map.get('z')); // "26"
map.keySet().forEach(key -> map.merge(key, -1, (prv, one) -> prv * one)); // reassign all variables
System.out.println(map.get('a')); // "-1" // individual variable still accessible
System.out.println(map.get('z')); // "-26"
map.put('b', 99); // modify variables selectively
System.out.println(map.get('b')); // "99" // individual variable still accessible
map.put('c', 0);
System.out.println(map.get('c')); // "0" // value in collection updates with value in variable
我不知道你为什么想要这个逻辑,但我认为你应该封装它。出于这个原因,单身可能不是最佳选择。最好编写一个具有所需逻辑的单独类。Map
@NoArgsConstructor
public final class Data {
private final int[] arr = new int[26];
{
for (int i = 0; i < arr.length; i++)
arr[i] = i;
}
public int getA() { return arr[getOffs('a')]; }
public int getB() { return arr[getOffs('b')]; }
public int getC() { return arr[getOffs('c')]; }
// other getters
public int getZ() { return arr[getOffs('z')]; }
public void setA(int a) { arr[getOffs('a')] = a; }
public void setB(int b) { arr[getOffs('b')] = b; }
public void setC(int c) { arr[getOffs('c')] = c; }
// other setters
public void setD(int d) { arr[getOffs('d')] = d; }
public void update(IntFunction<Integer> func) {
for (int i = 0; i < arr.length; i++)
arr[i] = func.apply(arr[i]);
}
private static int getOffs(char ch) { return Character.toLowerCase(ch) - 'a'; }
}
因此,客户端代码将如下所示:
Data data = new Data();
data.update(val -> val + 1); // change value of all variables
System.out.println(data.getA()); // "1" // individual variable still accessible
System.out.println(data.getZ()); // "26"
data.update(val -> val * -1); // reassign all variables
System.out.println(data.getA()); // "-1" // individual variable still accessible
System.out.println(data.getZ()); // "-26"
data.setB(99); // modify variables selectively
System.out.println(data.getB()); // "99" // individual variable still accessible
data.setC(0);
System.out.println(data.getC()); // "0" // value in collection updates with value in variable
0赞
Reilas
7/25/2023
#2
"...使用 Array / ArrayList 来完成上述所有操作是可行的,但同时我仍然希望保留变量名称。
没有语法;你必须使用一个类,或者像你提到的一个数组或列表。
正如其他人所提到的,您可以使用 Map,然后使用键作为变量名称。
通常,您要查找的内容将是脚本语言的一部分。
Java 是一种编程语言,所以它没有你要找的捷径。
Python 有很多这样的快捷方式。
它可能具有您正在寻找的语法,我不确定。
研究不同类型的脚本语言,你可能会发现这个确切的抽象。
我相信实现这一点的最简单方法是将值存储在数组中。
随后,只需为每个所需的过程创建一个方法。
class Values {
int[] values;
Values(int[] values) {
this.values = values;
}
void set(char letter, int value) {
switch (letter) {
case 'a' -> values[0] = value;
case 'b' -> values[1] = value;
case 'c' -> values[2] = value;
// ...
case 'z' -> values[25];
default -> throw new IllegalArgumentException();
}
}
int get(char letter) {
return switch (letter) {
case 'a' -> values[0];
case 'b' -> values[1];
case 'c' -> values[2];
// ...
case 'z' -> values[25];
default -> throw new IllegalArgumentException();
};
}
}
评论
Map<Character, SomeOtherType>