Can someone tell me why this code has a hole at: array[0][4]?
public class Random{
public static void main (String []args){
String [][] array={{"This is a test. A hole here"}};
for(int i=0;i<array.length;i++){
String temp=array[i][0];
array[i]=temp.split("[\\:., ]");
}
System.out.print(array[0][4]);
}
}
Yet, when I add a plus sign to the delimiters(“[\:., ]+”), I get the correct output.
public class Random{
public static void main (String []args){
String [][] array={{"This is a test. A hole here"}};
for(int i=0;i<array.length;i++){
String temp=array[i][0];
array[i]=temp.split("[\\:., ]+");
}
System.out.print(array[0][4]);
}
}
Is there a reason why the plus sign removes this hole and solve this problem? I am open to any suggestions or comments. Yes, I am a novice.
With
array[i]=temp.split("[\\:., ]");your string is splitted here:So you get an empty string at
array[4].With
array[i]=temp.split("[\\:., ]+");it will combine “. ” into one “split point” and because of that it will not split in between.