Auf Thema antworten

[code=java]

public class ArrayTest {


    private static String[] strings = { "a", "b", null, "c", null, null, "d" };


    public static void main(String[] args) {


        int i = 0;

        int j = strings.length - 1;


        while (i < j) {

            if (strings[i] == null && strings[j] != null) {

                strings[i] = strings[j];

                strings[j] = null;

            }

            if (strings[i] != null) {

                i++;

            }

            if (strings[j] == null) {

                j--;

            }

        }


        System.out.println(Arrays.toString(strings));

    }


}

[/code]

Laufzeit O(n).



Oben