将一个数组分成两个数组

4
我需要将矩阵元素放入一个数组中,然后需要先排序奇数,再排序偶数。
示例:这是一个数组: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++;
  }
}

问题在于这两个数组分别存储奇数和偶数,但我不知道它们的长度。如果我使用整个数组的大小,那么在奇数数组中放置偶数或在偶数数组中放置奇数时,剩余位置将得到零。 你会怎么做呢? 谢谢。

1
你可以使用集合框架吗? - Alp
是的,但我不知道如何使用集合。 - zeus
1
你可以在双重循环中保持偶数元素(或奇数元素或两者皆有)数量的累加计数。注意循环j的错别字,应该是 j<mat[i] - fresidue
@samke 请看下面的回答。 - Alp
6个回答

4
如果您能使用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
这里有几种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);

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

尽管存在未关闭的括号,但你正在使用原始类型,在泛型世界中这是一个致命错误。 - Makoto

1

数组大小没有问题,因为每个数组(array1array2)中都有足够的空间来容纳所有数字,并且您知道每个数组中元素的数量(cnt1cnt2)。因此,在循环之后,您可以将仅有效的元素复制到一个新数组中,如下所示:

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

Arrays.copyof(..) 参考资料


0

谢谢大家

会员 @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];

-1
你也可以使用ArrayList,它的大小是动态的但速度稍慢一些。 这解决了零的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接