Java中如何高效批量为多个变量赋值表格对应值?
解决方案
针对你遇到的Java基本类型无法批量赋值、数组索引易混淆的问题,给你几个实用的处理方式:
1. 自定义统计类(最直观)
把所有统计变量封装成一个类的成员字段,然后写一个批量加载的方法,把重复的parseInt逻辑收拢到类里,主代码里只用调用方法,后续使用变量时直接用字段名,完全不用记索引:
class MatchStats { public int wins; public int losses; public int ties; public int trueTies; public int p1Wins; public int p1Losses; public int p1Ties; public int p1TrueTies; public int p2Wins; public int p2Losses; public int p2Ties; public int p2TrueTies; // 批量从字符串数组加载数据 public void loadFromArray(String[] grabbedLine) { wins = Integer.parseInt(grabbedLine[0]); losses = Integer.parseInt(grabbedLine[1]); ties = Integer.parseInt(grabbedLine[2]); trueTies = Integer.parseInt(grabbedLine[3]); p1Wins = Integer.parseInt(grabbedLine[4]); p1Losses = Integer.parseInt(grabbedLine[5]); p1Ties = Integer.parseInt(grabbedLine[6]); p1TrueTies = Integer.parseInt(grabbedLine[7]); p2Wins = Integer.parseInt(grabbedLine[8]); p2Losses = Integer.parseInt(grabbedLine[9]); p2Ties = Integer.parseInt(grabbedLine[10]); p2TrueTies = Integer.parseInt(grabbedLine[11]); } }
使用示例:
String[] grabbedLine = Grabber.nextLine().split(","); MatchStats stats = new MatchStats(); stats.loadFromArray(grabbedLine); // 后续修改时直接用字段名,清晰无歧义 if (Your_Attack > Their_Defense) { stats.p1TrueTies += 1; }
2. 数组+常量索引(最简单)
如果不想写类,可以给每个数组索引定义对应的常量,这样后续使用时不用记数字,直接用常量名:
// 提前定义常量对应每个统计项的索引 public static final int WINS = 0; public static final int LOSSES = 1; public static final int TIES = 2; public static final int TRUE_TIES = 3; public static final int P1_WINS = 4; public static final int P1_LOSSES = 5; public static final int P1_TIES = 6; public static final int P1_TRUE_TIES = 7; public static final int P2_WINS = 8; public static final int P2_LOSSES = 9; public static final int P2_TIES = 10; public static final int P2_TRUE_TIES = 11; // 批量赋值 int[] stats = new int[12]; String[] grabbedLine = Grabber.nextLine().split(","); for (int i = 0; i < 12; i++) { stats[i] = Integer.parseInt(grabbedLine[i]); } // 修改时用常量名,不用记索引 if (Your_Attack > Their_Defense) { stats[P1_TRUE_TIES] += 1; }
3. 枚举+Map容器(更灵活)
如果需要更灵活的扩展(比如后续新增统计项),可以用枚举定义所有统计类型,搭配EnumMap存储值,既能批量赋值,又能通过枚举名明确操作的统计项:
// 枚举定义所有统计类型 enum StatType { WINS, LOSSES, TIES, TRUE_TIES, P1_WINS, P1_LOSSES, P1_TIES, P1_TRUE_TIES, P2_WINS, P2_LOSSES, P2_TIES, P2_TRUE_TIES } // 统计容器类 class Stats { private final Map<StatType, Integer> values = new EnumMap<>(StatType.class); public Stats() { // 初始化所有统计值为0 for (StatType type : StatType.values()) { values.put(type, 0); } } // 批量加载数据 public void loadFromArray(String[] grabbedLine) { StatType[] types = StatType.values(); for (int i = 0; i < types.length; i++) { values.put(types[i], Integer.parseInt(grabbedLine[i])); } } // 增加统计值 public void increment(StatType type) { values.put(type, values.get(type) + 1); } // 获取统计值 public int get(StatType type) { return values.get(type); } }
使用示例:
String[] grabbedLine = Grabber.nextLine().split(","); Stats stats = new Stats(); stats.loadFromArray(grabbedLine); // 修改时直接指定枚举类型,清晰明了 if (Your_Attack > Their_Defense) { stats.increment(StatType.P1_TRUE_TIES); }
这些方案都比你最初的逐个赋值或者裸数组更高效、更易维护,完全不用依赖Java不支持的指针特性。
内容的提问来源于stack exchange,提问作者Bloo
相关产品推荐
相关产品推荐

