0

I want create a method in java that will take a single dimensional array and shift all the non-zero numbers to the left of the array. I've tried some ways but nothing is working. Can somebody help me? Examples are given below for your reference:

int[] x = {0,8,0,7,0,0,2};
answer = {8,7,2,0,0,0,0}

int[] y = {5,0,0,0,1,0,3};
answer = {5,1,3,0,0,0,0}

That's what I tried:

import java.util.Arrays;

public class Board
    {

    public static final int EMPTY = 0;

    public static boolean alterOneLine(int[] x)
    {
        int[] temp = new int[x.length];
        for(int i=0;i<x.length;i++)
        {
            if (x[i]!=EMPTY)
                temp[i]=x[i];
        }
        if(Arrays.equals(x,temp))
            return false;
        else
            return true;
    }
}
2
  • Your code doesn't require a shift of the array because you're implementing a method that returns true if a given array is shifted. Your code is essentially just creating a copy of the original array and does no shift. You have to keep an additional counter for temp that you only increment when assigning a new value, e.g. temp[tmpCounter++] = x[i] Commented Feb 6, 2018 at 21:44
  • Stop vandalizing your post. If you want to ask another question, post it separately. Commented Feb 6, 2018 at 23:01

2 Answers 2

5

Just iterate and move the non-zero numbers to the beginning:

for (int i = 0, dest = 0; i < arr.length; i++) {
    if (arr[i] != 0) {
        int val = arr[i];
        arr[i] = 0;
        arr[dest++] = val;
    }
}

Or you can make a copy and rely on the array initialization defaults for the zeros:

int[] copy = new int[arr.length];
for (int i = 0, dest = 0; i < arr.length; i++) {
    if (arr[i] != 0) {
        copy[dest++] = arr[i];
    }
}

Alternatively, you can use a boxed stream with a custom sort:

int[] copy = Arrays.stream(arr)
        .boxed()
        .sorted(Comparator.comparing(i -> i == 0))
        .mapToInt(Integer::intValue)
        .toArray();

Or you can use Guava's Ints.asList() and sort the array in-place:

Ints.asList(arr).sort(Comparator.comparing(i -> i == 0));
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry for edit - I totally forgot about initializing primitive and arrays! Up voting.
0

As I said in my comment, your method doesn't require shifting the array because the method only checks if a given is already shifted (and returns false in that case. But for the sake of answering, here is the corrected version:

import java.util.Arrays;

public class Board {

    public static final int EMPTY = 0;

    public static boolean alterOneLine(int[] x) {
        int[] temp = new int[x.length];
        int tempCounter = 0;
        for (int i = 0; i < x.length; i++) {
            if (x[i] != EMPTY)
                temp[tempCounter++] = x[i];
        }
        if (Arrays.equals(x, temp))
            return false;
        else
            return true;
    }

    public static boolean alterOneLine2(int[] x) {
        boolean zeroSeen = false;
        for (int i = 0; i < x.length; i++) {
            if (x[i] != 0 && zeroSeen) {
                return true;
            }
            if (x[i] == 0) {
                zeroSeen = true;
            }
        }
        return false;
    }

    public final static void main(String[] args) {
        System.out.println(alterOneLine(new int[]{0,8,0,7,0,0,2}));
        System.out.println(alterOneLine(new int[]{8,7,2,0,0,0,0}));
        System.out.println(alterOneLine(new int[]{5,0,0,0,1,0,3}));
        System.out.println(alterOneLine(new int[]{5,1,3,0,0,0,0}));
        System.out.println(alterOneLine2(new int[]{0,8,0,7,0,0,2}));
        System.out.println(alterOneLine2(new int[]{8,7,2,0,0,0,0}));
        System.out.println(alterOneLine2(new int[]{5,0,0,0,1,0,3}));
        System.out.println(alterOneLine2(new int[]{5,1,3,0,0,0,0}));
    }
}

Your error was using the same index when assiging a value to temp instead of an individual counter that is only increased if you shift a value to the left.

I also added a second method with the same functionality but without creating a temporary array and supsequent call of Arrays#equals (two iterations over the whole array instead of one with my new method).

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.