4

I need elements of matrix put into an array, then i need to sort first odd numbers and then even numbers Example: This is array: 5, 9, 1, 2, 3, 8, 4. output: 1,3,5,9 ; 2,4,8

This is my code:

int[] array=new int[mat.length*mat[0].length];
int cnt=0;

for(int i=0; i<mat.length; i++)
{
  for(int j=0; j<mat[0].length; j++)
  {
    array[cnt]=mat[i][j];
    cnt++;
  }
}
int cnt1=0;
int cnt2=0;
int[] array1=new int[array.length];
int[] array2=new int[array.length];
for(int i=0; i<array.length; i++)
{
  if(array[i]%2==0)
  {
    array1[br1]=array[i];
    cnt1++;
  }
  else
  {
    array2[br2]=array[i];
    cnt2++;
  }
}

The problem is this two arrays for odd and even numbers, because i don't know their length and if i put the size of whole array, then i will get zeros for remaining places in odd array for number that is even and vice versa. How would you do this? Thank you

4
  • 1
    are you allowed you use Collections ? Commented Jun 29, 2015 at 21:59
  • yes, but i don't know how to use collections Commented Jun 29, 2015 at 22:05
  • 1
    You can keep a running count of the number of even elements (or odd elements, or both) in the double-loop. Watch out for the typo in the loop over j. It should be j<mat[i] Commented Jun 29, 2015 at 22:08
  • @samke See my answer below please Commented Jun 29, 2015 at 22:14

6 Answers 6

4

If you can use List you can do

List<Integer> even = new ArrayList<>();
List<Integer> odd = new ArrayList<>();

for(int i=0; i<mat.length; i++) {
    for(int j=0; j<mat[0].length; j++) {
        if (mat[i][j] % 2 == 0)
            even.add(mat[i][j]);
        else
            odd.add(mat[i][j]);
    }
}

Collections.sort(even);
Collections.sort(odd);

odd.addAll(even);

for (int v: odd){
    System.out.println(v);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here are a couple of Java 8 solutions (which are far more straightforward):

With two passes of the stream, it's filter and sort.

final int[] ints = {5, 9, 1, 2, 3, 8, 4};

int[] oddArray = Arrays.stream(ints).filter(x -> x % 2 != 0).sorted().toArray();
int[] evenArray = Arrays.stream(ints).filter(x -> x % 2 == 0).sorted().toArray();

System.out.println(Arrays.toString(oddArray));
System.out.println(Arrays.toString(evenArray));

With one pass of the stream, you'd want to use collections so that you don't have to deal with proper sizing of the array. You still have to sort it, though.

final int[] ints = {5, 9, 1, 2, 3, 8, 4};

List<Integer> oddList = new ArrayList<>();
List<Integer> evenList = new ArrayList<>();

Arrays.stream(ints).forEach(e -> {
    if(e % 2 != 0) {
        oddList.add(e);
    } else {
        evenList.add(e);
    }
});
Collections.sort(oddList);
Collections.sort(evenList);

System.out.println(oddList);
System.out.println(evenList);

Comments

1
 // This is how you instantiate collections.
 List odds = new ArrayList(); 
 List evens = new ArrayList();

 ...
 if(array[i]%2==0)
  {
    // Here you add a new item to the collection for even numbers
    evens.add(array[i];
  }
  else
  {
    // Here you add a new item to the collection for odd numbers
    odds.add(array[i]);
  }


...

// And finally this is how you get arrays out of collections
int[] oddArray = odds.toArray(new int[]);
int[] evenArray = evens.toArray(new int[]);

1 Comment

Notwithstanding the unclosed paren, you're using raw types which is a cardinal sin in the world of generics.
1

There is not problem with array size as there is enough space in each array (array1 and array2) to hold all the numbers and you know the count (cnt1 and cnt2) of elements in each array. Thus after loop you can copy only valid elements to a new array as below:

int[] even = Arrays.copyOf(array1, cnt1);
int[] odd = Arrays.copyOf(array2, cnt2);

Arrays.copyof(..) reference

Comments

0

Thank you all

member @fresidue remind me to this which help me to solve this problem

int cnt=0;
    int cnt1=0;
    int cnt2=0;
    for(int i=0; i<mat.length; i++)
    {
      for(int j=0; j<mat[0].length; j++)
      {
        array[cnt]=mat[i][j];
        cnt++;
        if(mat[i][j]%2==0)
          cnt1++;
        else
          cnt2++;
      }
    }

    int[] array1=new int[cnt1];
    int[] array2=new int[cnt2];

Comments

-1

You could also use an ArrayList, this has a dynamic size but is a bit slower. This solves the problem of the zeros

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.