2

Assume there is only one null in an array. I am trying to move it right to the end using a for loop. this is what i tried.

    String example[] = new String[5];
    example[0] = "a";
    example[1] = null; 
    example[2] = "c"; 
    example[3] = "d"; 
    example[4] = "e";

I want the output to be : a,c,d,e,null. I want to able to move the null to the end regardless of its index using a FOR loop.

This is what i tried

    String asd[] = new String[creatureList.length];

    for (int i = 0 ; i < creatureList.length; i++) {
        if (creatureList[i] != null){

        asd[i] = creatureList[i];



        }
        creatureList = asd;
2
  • There are several ways to do it. Is your approach not working or are you looking for a better approach (better in terms of?) ? Commented Sep 11, 2014 at 12:48
  • my approach is not working Commented Sep 11, 2014 at 12:55

6 Answers 6

2

Just search for the null element and if found, shift all elements 1 position to the left. This code works even if there are more than 1 null elements in the array.

public class Test10 {

    public static void main(String[] args) {
        String example[] = new String[5];
        example[0] = "a";
        example[1] = null; 
        example[2] = "c"; 
        example[3] = "d"; 
        example[4] = "e";

        for (int j=0; j<example.length; j++){
            if (example[j]==null){
                for (int k=j+1; k<example.length; k++){
                    example[k-1] = example[k];
                }
                example[example.length-1] = null;
                break;
            }
        }

        for (int j=0; j<example.length; j++){
            System.out.println(example[j]);
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

You can just use the principle of bubble sort in here. Try this

String example[] = new String[5];
example[0] = "a";
example[1] = null; 
example[2] = "c"; 
example[3] = "d"; 
example[4] = "e"; 

for(int i=0; i < example.length - 1; ++i) {
    if(example[i] == null) {
        example[i] = example[i+1];
        example[i+1] = null;
    }
}

for(String s : example)
    System.out.print(s + " ");

If you only need to print the values that way, try this

for(String s : example)
    if(s != null)
        System.out.print(s + " ");
System.out.print(null + " ");

3 Comments

This wouldn't be the most efficient way of doing it, but still should work.
I figured that out, but I almost never find a situation to use bubble sort (or at least its main idea). I just got carried out. =D
that's because it's known as one of the 'simple' sorts - if you're learning about advanced sorts, you might want to look at the 'advanced sorts'
0

A possible solution without creating a new Array would be to move the elements. Example, assuming that example is an Array:

boolean found = false;

for(int i = 0; i < example.length; i++) {
  if(found)
    example[i-1] = example[i];
  if(example[i] == null)
    found = true;
  if(i == example.length - 1 && found)
    example[i] = null;
}

I want to add that this would be a lot easier if you would use Collections (for example an ArrayList), I highly recommend using them instead of Arrays if performance is not a huge factor.

Comments

0

I'd have a different implementation for ArrayLists (as .remove() is handy), but here we are with a bounded array:

for(int n = 0; n < array.length; n++) {
   if(null == array[n]) {
        // to avoid issues with references, unsure if necessary
        String s = array[n + 1];
        array[n] = array[s];
        array[n + 1] = null;
    }
}

Comments

0

Here's a solution that is based on your assumption that there's only one null:

for (int i=1; i<example.length; i++) {
    if (example[i-1] == null) {
        example[i-1] = example[i];
        example[i] = null;
    }
}

Comments

0

I think the answers before me were not correct, so here is a code that works and puts all the nulls at the end of a array.

public static void moveNullToTheEnd( Object[] arr ) 
{
    
    Object[] temp = new Object[arr.length];
    int counter = 0;
    int nullcounter = arr.length-1;
    for(int i= 0; i < arr.length; i++) {
        if(arr[i] == null){
            temp[nullcounter] = null;
            nullcounter--;
            
        }
        else {
            temp[counter] = arr[i];
            counter++;
            
        }
    }
    arr = temp;
    
    System.out.println(Arrays.toString(arr));
    
}   

    
public static void main (String[] args)
{
      Object [] test = new Object[] {1, 2, null, null, 4};
      moveNullToTheEnd(test);
}

1 Comment

Why you think the accepted answer is not correct? I have tested it and it seems to work as intended. This is a very old question and probably doesn't need anymore answers unless you have something new to add.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.