Split an array into two arrays
I need the elements of a matrix placed in an array, then I need to sort the first odd numbers and then the even numbers Example: this is an 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 that these are two arrays for odd and even numbers, because I don't know their length, and if I put in the size of the whole array, then I get zeros for the remaining places in the odd array for a number that is equal and vice versa. How would you do it? Thanks you
source to share
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);
}
source to share
There is no problem with the size of the array, as there is enough space in each array ( array1 and array2 ) to hold all the numbers and you know the number ( cnt1 and cnt2 ) of elements in each array. This way, after the loop, you can copy only valid elements to the new array as shown below:
int[] even = Arrays.copyOf(array1, cnt1);
int[] odd = Arrays.copyOf(array2, cnt2);
Arrays.copyof (..) link
source to share
Here are some Java 8 solutions (which are much more straightforward):
With two flow passages, it filters and sorts.
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 stream pass, you want to use collections so that you don't have to deal with the correct size of the array. You will still have to sort it.
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);
source to share
// 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[]);
source to share
Thanks everyone
member @fresidue reminds me of this which helps me 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];
source to share