0

I want to shift each elements in array to left if there is a null. E.g

    public static void main(String[] args) {
    String asd[] = new String[5];
    asd[0] = "zero";
    asd[1] = "one";
    asd[2] = null;
    asd[3] = "three";
    asd[4] = "four;

I want the output to be

  zero, one, three, four.

The length should also be adjusted

How can i do this using loops? I tried using if statements to check if an element is not null copy that value to another array. But i dont know how to copy if there is a null.

3 Answers 3

1

Given the kind of question, I suppose you want a simple, loop only and array only based solution, to understand how it works.

You have to iterate on the array, keeping an index of the new insertion point. At the end, using that same index, you can "shrink" the array (actually copy to a new smaller array).

String[] arr = {"a","b",null,"c",null,"d"};

// This will move all elements "up" when nulls are found
int p = 0;
for (int i = 0; i < arr.length; i++) {
  if (arr[i] == null) continue;
  arr[p] = arr[i];
  p++;
}

// This will copy to a new smaller array
String[] newArr = new String[p];
System.arraycopy(arr,0,newArr,0,p);

Just tested this code.

EDIT :

Regarding the possibility of shrinking the array without using System.arraycopy, unfortunately in Java arrays size must be declared when they are instantiated, and can't be changed (nor made bigger nor smaller) after.

So if you have an array of length 6, and find 2 nulls, you have no way of shrinking it to a length of 4, if not creating a new empty array and then copying elements.

Lists can grow and shrink, and are more handy to use. For example, the same code with a list would be :

String[] arr = {"a","b",null,"c",null,"d"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
Iterator<String> iter = list.iterator();
while (iter.hasNext()) if (iter.next() == null) iter.remove();
System.out.println(list);
Sign up to request clarification or add additional context in comments.

2 Comments

hi, thanks for that. Although i was wondering if instead of using system.arraycopy will i be able to do it in a for loop? you dont have to do the code, unless you want to, but if possible can you give me a quick idea on how i should do it?
Edited the post to explain why you need System.arraycopy and how instead Lists work.
1

Try:

int lengthNoNull = 0;
for(String a : asd) {
    if(a != null) {
        lengthNoNull++;
    }
}
String[] newAsd = new String[lengthNoNull];
int i = 0;
for(String a : asd) {
    if(a != null) {
        newAsd[i++] = a;
    }
}

Comments

0

Piece of code using only arrays.

    String[] x = {"1","2","3",null,"4","5","6",null,"7","8","9"};
    String[] a = new String[x.length];
    int i = 0;
    for(String s : x) {
        if(s != null) a[i++] = s;
    }
    String[] arr = Arrays.copyOf(a, i);

Or this:

    String[] xx = {"1","2","3",null,"4","5","6",null,"7","8","9"};
    int pos = 0, i = 0;
    String tmp;
    for(String s : xx) {
        if(s == null) {
            tmp = xx[pos];
            xx[pos] = s;
            xx[i] = tmp;
            pos++;
        }
        i++;
    }
    String[] arr = Arrays.copyOfRange(xx, pos, xx.length);

Comments

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.