Java 将一个数组拆分为两个数组

Java 将一个数组拆分为两个数组,java,Java,我需要将矩阵的元素放入一个数组中,然后我需要先对奇数排序,然后对偶数排序 示例:这是数组:5,9,1,2,3,8,4。产量:1,3,5,9;2,4,8 这是我的代码: 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];

我需要将矩阵的元素放入一个数组中,然后我需要先对奇数排序,然后对偶数排序 示例:这是数组:5,9,1,2,3,8,4。产量:1,3,5,9;2,4,8

这是我的代码:

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++;
  }
}
int[]数组=新的int[mat.length*mat[0].length];
int-cnt=0;

对于(inti=0;i您也可以使用ArrayList,它有一个动态大小,但速度稍慢。
这解决了零的问题数组大小没有问题,因为每个数组(array1array2)中有足够的空间来容纳所有数字,并且您知道计数(cnt1cnt2)每个数组中的元素数。因此,循环后,您只能将有效元素复制到新数组,如下所示:

 // 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[]);
int[] even = Arrays.copyOf(array1, cnt1);
int[] odd = Arrays.copyOf(array2, cnt2);

Arrays.copyof(..)

如果您可以使用
列表
您可以

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);
}
List偶数=new ArrayList();
List奇数=新的ArrayList();

对于(inti=0;i,这里有几个Java 8解决方案(它们更简单):

流经过两次,即过滤和排序

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));
通过流的一次传递,您可能希望使用集合,这样就不必处理数组的适当大小。不过,您仍然需要对其进行排序

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);
final int[]int={5,9,1,2,3,8,4};
List oddList=new ArrayList();
List evenList=new ArrayList();
Arrays.stream(ints).forEach(e->{
如果(e%2!=0){
增补(e);
}否则{
增加(e);
}
});
集合。排序(oddList);
集合。排序(evenList);
系统输出打印项次(oddList);
System.out.println(evenList);
谢谢大家

@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];
int cnt=0;
int cnt1=0;
int cnt2=0;

对于(int i=0;i您允许您使用集合吗?是的,但我不知道如何使用集合您可以对偶数元素(或奇数元素,或两者)的数量进行连续计数在双循环中。注意j上方循环中的打字错误。应该是
j@samke请看下面我的答案,站在这里,你使用的是原始类型,这是泛型世界中的一大罪过。