下面写的代码是正确的,但是我想缩短这段代码。
用Java编写程序,以在一维数组中输入10个数字,并以所有偶数后跟所有奇数的方式排列它们。
int a[] = new int[6];
int b[] = new int[6];
int i, j;
int k = 0;
System.out.println("enter array");
for (i = 0; i < 6; i++) {
a[i] = sc.nextInt();
}
for (j = 0; j < 6; j++) {
if (a[j] % 2 == 0) {
b[k] = a[j];
k++;
}
}
for (j = 0; j < 6; j++) {
if (a[j] % 2 != 0) {
b[k] = a[j];
k++;
}
}
System.out.println("out-put");
for (i = 0; i < 6; i++) {
System.out.println(b[i]);
}
我可以在一个for循环中而不是两个for循环中排列偶数和奇数吗?我正在使用两个for循环将偶数和奇数传输到b[]
数组中。请缩短代码。一个用于循环遍历以检查偶数,另一个用于检查奇数。
答案 0 :(得分:3)
这是一个简单的程序。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Momir Sarac
*/
public class GroupByEvenAndOddNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// create a collection
List<Integer> listOfNumbers = new ArrayList<>();
// do code within a loop for 10 times
for(int i=0;i<10;i++)
{
//print to screen this text
System.out.println("Input your number:");
//get next input integer
int number = scanner.nextInt();
// add it to collection
listOfNumbers.add(number);
}
// sort this collection, list of numbers
// convert all numbers(positive and negative ) within to 0 or 1 depending whether or not they are even or odd and sort them accordignaly.
Collections.sort(listOfNumbers, Comparator.comparingInt(n -> Math.floorMod(n, 2)));
//print sorted collection
System.out.println("Ordered list ..." + listOfNumbers);
}
}
答案 1 :(得分:2)
在此版本中,它将偶数复制到开头,将奇数复制到结尾。
static int[] sortEvenOdd(int... nums) {
int even = 0, odd = nums.length, ret[] = new int[nums.length];
for (int num : nums)
if (num % 2 == 0)
ret[even++] = num;
else
ret[--odd] = num;
return ret;
}
public static void main(String[] args) {
int[] arr = {1, 3, 2, 4, 7, 6, 9, 10};
int[] sorted = sortEvenOdd(arr);
System.out.println(Arrays.toString(sorted));
}
打印
[2, 4, 6, 10, 9, 7, 3, 1]
答案 2 :(得分:0)
此代码将帮助您分隔偶数和奇数。
// java code to segregate even odd
// numbers in an array
public class GFG {
// Function to segregate even
// odd numbers
static void arrayEvenAndOdd(
int arr[], int n)
{
int i = -1, j = 0;
while (j != n) {
if (arr[j] % 2 == 0)
{
i++;
// Swapping even and
// odd numbers
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
j++;
}
// Printing segregated array
for (int k = 0; k < n; k++)
System.out.print(arr[k] + " ");
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 3, 2, 4, 7,
6, 9, 10 };
int n = arr.length;
arrayEvenAndOdd(arr, n);
}
}
答案 3 :(得分:0)
由于您没有必要将偶数和奇数本身分别在数组的一半进行排序,因此您可以在输入时将它们分配给关联的数组部分。
因此,您只需要使用两个“计数器”变量,一个在左边,从零开始递增,一个在右边,从数组长度减一开始递减,一个递增。然后,您可以添加您的数字,检查是否为偶数,将左计数器增加,则分配为奇数;如果奇数,右计数器减少,则分配。循环执行此操作,直到您的左计数器大于右计数器。
我创建了一个简单的示例,在将String解析为int时,我没有检查NumberFormatException
:
import java.util.Arrays;
import java.util.Scanner;
public class SortedArrayInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter length of array: ");
final int arrayLength = Integer.parseInt(scanner.nextLine());
int intArray[] = new int[arrayLength];
for (int l = 0, r = arrayLength - 1; l <= r; ) {
System.out.print("Enter new array value: ");
int v = Integer.parseInt(scanner.nextLine());
intArray[v % 2 == 0 ? l++ : r--] = v;
}
System.out.println("Output: " + Arrays.toString(intArray));
}
}
示例输入/输出:
Enter length of array: 6
Enter new array value: 1
Enter new array value: 2
Enter new array value: 3
Enter new array value: 4
Enter new array value: 5
Enter new array value: 6
Output: [2, 4, 6, 5, 3, 1]
答案 4 :(得分:0)
我建议阅读流,它们将使您的收集处理变得容易得多
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
numbers.add(7);
numbers.add(8);
numbers.add(9);
numbers.add(0);
//this way you simply traverse the numbers twice and output the needed ones
System.out.println(numbers.stream()
.filter(x->x%2==0)
.collect(Collectors.toList()));
System.out.println(numbers.stream()
.filter(x->x%2==1)
.collect(Collectors.toList()));
//this way you can have the numbers in two collections
numbers.forEach(x-> x%2==0? addItToEvenCollection : addItToOddCollection);
//this way you will have a map at the end. The boolean will tell you if the numbers are odd or even,
// and the list contains the numbers, in order of apparition in the initial list
numbers.stream().collect(Collectors.groupingBy(x->x%2==0));
答案 5 :(得分:0)
一种检查数字是否为偶数的高性能方法是使用 如果((x&1)== 0)