0

I have this array: String[][] hej = {{"9.8", "0", "hi", "0"}, {"0", "3.4", "yes", "no"}};

And I want to create a new array without all the zeros.

I started to create a new array:

String[][] zero = new String[hej.length][hej[0].length];

I have tried to only insert the elements which are not zeros with this for loop:

for(int c = 0; c < zero.length; c++) {
    int i = 0;
    if(hej[i][c] != "0") {
    zero[i][c] = hej[i][c];

But it doesn't work and I cannot understand why.

If I do it without a for loop it's like this: ` if(hej[0][0] != "0") zero[0][0] = hej[0][0];

if(hej[0][1] != "0")
    zero[0][1] = hej[0][1];

if(hej[0][2] != "0")
    zero[0][2] = hej[0][2];

if(hej[0][3] != "0")
    zero[0][3] = hej[0][3];`

But then I still don't know how to make the array shorter without the spot for the removed zero.

  • Anyone who can help me understand why my for loop doesn't work and how I can make a for loop to go through the whole [][] array?

  • Anyone who can help me understand how to at the same time create a new dynamic array without the spots from the zeros?

Thanks!

3
  • Is your title supposed to say "array"? And can you just filter the inner lists? If you do it with a for-loop, you'll need 2 nested loops. The second loop checks the inner lists. Commented Mar 25, 2017 at 12:23
  • Yes sure. Sorry. Can I edit it? Commented Mar 25, 2017 at 12:25
  • You can quite easily. Press "edit" under your post. Commented Mar 25, 2017 at 12:25

1 Answer 1

0

Anyone who can help me understand why my for loop doesn't work and how I can make a for loop to go through the whole [][] array?

You have to iterate the two dimensional array with two loops like a for inside a for loop as follows

   public static void eliminateZerosWithStaticArray() throws Exception {
    String[][] hej = {{"9.8", "0", "hi", "0"}, {"0", "3.4", "yes", "no"}};
            int width = hej.length;
            int height = hej[0].length;
            String[][] zero = new String[width][height];

            for(int c=0; c < width; c++) {
                for(int d=0,i=0; d<height; d++) {
                    if(!"0".equals(hej[c][d])) {
                        zero[c][i] = hej[c][d];
                        i++;
                    }
                }
            }
            System.out.println("Printing the values within zero array ::: ");
            for(int i=0; i<zero.length; i++) {
                for(int j=0; j<zero[i].length; j++ ) {
                    System.out.println("The values are : "+ zero[i][j]);
                }
            }
    }

Anyone who can help me understand how to at the same time create a new dynamic array without the spots from the zeros?

That is where ArrayList comes into existence. Here is an answer on how to add elements to add elements dynamically into an array in java.

public static void eliminateZerosWithDynamicArray() throws Exception {
        String[][] hej = {{"9.8", "0", "hi", "0"}, {"0", "3.4", "yes", "no"}};
        int width = hej.length;
        int height = hej[0].length;
        List<List<String>> result = new ArrayList<List<String>>(width);

        //Iterate the original array
        for(int c=0; c < width; c++) {
            List<String> templist = new ArrayList<String>();
            for(int d=0; d<height; d++) {
                if(!"0".equals(hej[c][d])) {
                    templist.add(hej[c][d]);
                }
                result.add(templist);
            }
        }
        //Print the list content
        for(int c=0; c<result.size(); c++) {
            System.out.println("List content : "+result.get(c));
        }
    }
Sign up to request clarification or add additional context in comments.

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.