简体   繁体   中英

How to shift each elements in array using loops?

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

    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.

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);

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;
    }
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM