Java技术求助:如何检查字符串数组指定索引的null值并替换?
问题描述
尝试生成包含66个三元数字数组的集合并打印,输出中固定有两个数组包含null值。尝试多种检查方法均无效:
- 用
==运算符的if语句检查,代码块未执行,null值仍输出 - 用
contains()方法检查,抛出NullPointerException - 用
equals()方法检查,有时程序终止,有时代码块未执行
此外,试过Math.random()和Random对象两种随机数生成方式,参考相关帖子修改代码后问题依旧。
原代码
import java.util.*; import java.lang.*; public class TestingTests{ public static void main(String[] args) { obj.uniqueString(); } void uniqueString() { while (randomNum.size() < 66) { for (int index = 0; index <= 2; index++) { int n = obj.rng.nextInt(4)+1; numS[index] = Integer.toString(n); if (numS[index] == null) { n = obj.rng.nextInt(4)+1; numS[index] = Integer.toString(n); } else if (randomNum.size() == 66) { ArrayList randomList = new ArrayList(randomNum); System.out.println("This is the size of randomList: " + randomList.size()); Collections.sort(randomList); System.out.println("This is a randomList: " + randomList); break; } else randomNum.add(Arrays.toString(numS)); } } } int temp = 0, count = 0; static TestingTests obj = new TestingTests(); String[] numS = new String[3]; Random rng = new Random(); Set<String> randomNum = new HashSet<>(); ArrayList randomList = new ArrayList(randomNum); }
输出示例
This is the size of randomList: 66 This is a randomList: [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 2, 4], [1, 3, 1], [1, 3, 2], [1, 3, 3], [1, 3, 4], [1, 4, 1], [1, 4, 2], [1, 4, 3], [1, 4, 4], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 1, 4], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 2, 4], [2, 3, 1], [2, 3, 2], [2, 3, 3], [2, 3, 4], [2, 4, 1], [2, 4, 2], [2, 4, 3], [2, 4, 4], [3, 1, 1], [3, 1, 2], [3, 1, 3], [3, 1, 4], [3, 1, null], [3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 2, 4], [3, 3, 1], [3, 3, 2], [3, 3, 3], [3, 3, 4], [3, 4, 1], [3, 4, 2], [3, 4, 3], [3, 4, 4], [3, null, null], [4, 1, 1], [4, 1, 2], [4, 1, 3], [4, 1, 4], [4, 2, 1], [4, 2, 2], [4, 2, 3], [4, 2, 4], [4, 3, 1], [4, 3, 2], [4, 3, 3], [4, 3, 4], [4, 4, 1], [4, 4, 2], [4, 4, 3], [4, 4, 4]] Process finished with exit code 0
问题分析与解决方案
问题根源
- 数组未重置:
numS是类成员变量,多次循环生成三元组时,未重置数组所有元素。若某次循环只修改了部分元素,剩余位置保留初始的null,转成字符串后就会出现带null的数组。 - 添加时机错误:for循环每生成一个元素就将整个数组添加到集合,此时数组可能还未填满3个元素,直接导致带
null的不完整数组被加入集合。 - 逻辑判断错位:判断集合大小是否达到66的逻辑放在for循环内部,此时数组未生成完整,容易触发错误的终止逻辑。
修正方案
- 将
numS改为方法内的局部变量,每次生成三元组时重新初始化,避免残留旧值。 - 先完整生成3个元素的三元组,再添加到集合中。
- 调整循环逻辑,确保生成完整三元组后再判断集合大小,达到66时停止循环并输出结果。
修正后的代码
import java.util.*; public class TestingTests { public static void main(String[] args) { new TestingTests().uniqueString(); } void uniqueString() { Set<String> randomNum = new HashSet<>(); Random rng = new Random(); // 生成完整的三元组直到集合达到66个元素 while (randomNum.size() < 66) { String[] numS = new String[3]; // 每次循环重新初始化数组 // 填充完整的三个元素 for (int index = 0; index < 3; index++) { int n = rng.nextInt(4) + 1; numS[index] = Integer.toString(n); } // 添加完整的三元组字符串到集合 randomNum.add(Arrays.toString(numS)); } // 转换为列表并排序输出 ArrayList<String> randomList = new ArrayList<>(randomNum); Collections.sort(randomList); System.out.println("This is the size of randomList: " + randomList.size()); System.out.println("This is a randomList: " + randomList); } }
内容的提问来源于stack exchange,提问作者Coding Noob Unfiltered
相关产品推荐
相关产品推荐

