如何在Java中替换数组元素?

时间:2013-12-13 12:03:28

标签: java

我有这个数组:

array = [null, p, ;, z,null, OR, y] 

我想用任何其他值替换null值。我该怎么做?

2 个答案:

答案 0 :(得分:1)

for (pos=0;pos<array.length;pos++) {
    if(array[pos]==null) {
       array[pos]=xyz
    }
}

答案 1 :(得分:1)

喜欢这个

String[] values= {null,"p", ";","z",null, "OR","y"};        
List<String> list=new ArrayList<String>(Arrays.asList(values));
Collections.replaceAll(list, null, "newVal");
values = list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(values));