Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!
Welcome to Coding challenge Day 24: Problem 1! Be sure to post your answers, queries etc in the comments!
Problem: Push ‘null’ to end of String array
Sample: String [] array= {“Horse”, null, “Cow”, “Hen”, null, “Pig”};
Otuput: “Horse”, “Cow”, “Pig”, null, null
Solution:
package string;
public class Program014null_at_end {
public static void main (String [] args){
String [] array= {"Horse", null, "Cow", "Hen", null, "Pig"};
for(int i=0; i<array.length; i++){
if (array[i]==null){
for(int j=i+1; j<array.length; j++){
if(array[j]!=null){
array[i]=array[j];
array[j]= null;
break;
}
}
}
}
for(String s: array){
System.out.print(s +" ");
}
}
}
Happy Learning!!