将数组拆分为两个数组

时间:2022-02-28 08:17:52

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

我需要将矩阵元素放入数组中,然后我需要对第一个奇数进行排序,然后对偶数进行排序示例:这是数组:5,9,1,2,3,8,4。输出: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

问题是这两个奇数和偶数数组,因为我不知道它们的长度,如果我把整个数组的大小,那么我将得到奇数数组中剩余位数的零,即偶数,反之亦然。你会怎么做?谢谢

6 个解决方案

#1


4  

If you can use List you can do

如果你可以使用List,你可以做

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);
}

#2


1  

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

这里有几个Java 8解决方案(更直接):

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);

#3


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:

数组大小没有问题,因为每个数组(array1和array2)中有足够的空间来容纳所有数字,并且您知道每个数组中元素的计数(cnt1和cnt2)。因此,在循环之后,您只能将有效元素复制到新数组,如下所示:

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

Arrays.copyof(..) reference

Arrays.copyof(..)引用

#4


0  

 // 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[]);

#5


0  

Thank you all

谢谢你们

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

会员@fresidue提醒我这有助于我解决这个问题

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];

#6


-1  

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

您也可以使用ArrayList,它具有动态大小但速度稍慢。这解决了零的问题

#1


4  

If you can use List you can do

如果你可以使用List,你可以做

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);
}

#2


1  

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

这里有几个Java 8解决方案(更直接):

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);

#3


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:

数组大小没有问题,因为每个数组(array1和array2)中有足够的空间来容纳所有数字,并且您知道每个数组中元素的计数(cnt1和cnt2)。因此,在循环之后,您只能将有效元素复制到新数组,如下所示:

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

Arrays.copyof(..) reference

Arrays.copyof(..)引用

#4


0  

 // 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[]);

#5


0  

Thank you all

谢谢你们

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

会员@fresidue提醒我这有助于我解决这个问题

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];

#6


-1  

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

您也可以使用ArrayList,它具有动态大小但速度稍慢。这解决了零的问题