Hi i encountered this problem whereby when i initialized my String[], there seems to be a null in the String[] before i do anything. How do i initialized the String[] to be completely empty,i.e. without the null at the start?
The output for the following code is:
nullABC
nullABC
nullABC
nullABC
nullABC
public static void main(String[] args){
String[] inputArr = new String[5];
for (int i = 0; i< inputArr.length; i++){
inputArr[i] += "ABC";
}
for (int i = 0; i< inputArr.length; i++){
System.out.println(inputArr[i]);
}
}
}
A null reference is about as empty as a string array element can be. Note that there’s a big difference between a reference to the empty string and a null reference though. Just change your code to use simple assignment instead of +=.
If you need to do conditional concatenation elsewhere, use something like this:
Alternatively, you could use something like this:
and then have:
Or (yes, lots of available options):