如何在Java中显示数组中的某些数字?

如何在Java中显示数组中的某些数字?,java,arrays,sorting,numbers,Java,Arrays,Sorting,Numbers,我想在一行中只挑出正数,在一行中只挑出负数,但它们只能在文本中逐个显示。 这是我的密码: int[] array = {2, -5, 4, 12, 54, -2, -50, 150}; Arrays.sort(array); for (int i = 0; i < array.length; i++) { if (array[i] < 0) { System.out.println("Less than 0: " + array

我想在一行中只挑出正数,在一行中只挑出负数,但它们只能在文本中逐个显示。 这是我的密码:

int[] array = {2, -5, 4, 12, 54, -2, -50, 150};
    Arrays.sort(array);
    for (int i = 0; i < array.length; i++) {
        if (array[i] < 0) {
            System.out.println("Less than 0: " + array[i]);

        } else if (array[i] > 0) {
            System.out.println("Greater than 0: " + array[i]);
        }

    }
int[]数组={2,-5,4,12,54,-2,-50,150};
数组。排序(数组);
for(int i=0;i0){
System.out.println(“大于0:+array[i]);
}
}

您当前正在为每个元素打印一行(无论它是小于0还是大于0),相反,我将使用
IntStream
filter()
对所需元素进行筛选(并使用
收集器收集这些元素。joining()
)。像

对于每个循环和格式化io,使用一对
StringJoiner
(s)a
,可以获得相同的结果。像

int[] array = { 2, -5, 4, 12, 54, -2, -50, 150 };
Arrays.sort(array);
StringJoiner sjLess = new StringJoiner(", ");
StringJoiner sjGreater = new StringJoiner(", ");
for (int x : array) {
    if (x < 0) {
        sjLess.add(String.valueOf(x));
    } else if (x > 0) {
        sjGreater.add(String.valueOf(x));
    }
}
System.out.printf("Less than 0: %s%n", sjLess.toString());
System.out.printf("Greater than 0: %s%n", sjGreater.toString());
int[]数组={2,-5,4,12,54,-2,-50,150};
数组。排序(数组);
细木工sjLess=新细木工(“,”);
细木工sjGreater=新细木工(“,”);
for(int x:array){
if(x<0){
sjLess.add(String.valueOf(x));
}如果(x>0),则为else{
sjgreer.add(String.valueOf(x));
}
}
System.out.printf(“小于0:%s%n”,sjLess.toString());
System.out.printf(“大于0:%s%n”,sjmorer.toString());

执行以下操作:

Arrays.sort(array);
String negative = "Less than 0: ";
String positive = "Greater than 0: ";
for (int i = 0; i < array.length; i++) {
    if (array[i] < 0) {
        negative.concat(array[i] + ",");
    } 
    else (array[i] > 0) {
       positive.concat(array[i] + ",");
    }
}
System.out.println(positive);
System.out.println(negative);
array.sort(数组);
字符串负=“小于0:”;
字符串正数=“大于0:”;
for(int i=0;i0){
concat(数组[i]+“,”);
}
}
系统输出打印项次(正);
系统输出打印项次(负片);

将值存储在字符串中,然后在for循环后打印它们。

我尝试尽可能少地更改代码

int[] array = { 2, -5, 4, 12, 54, -2, -50, 150 };
Arrays.sort(array);
boolean firstHalf = true;
System.out.print("Less than 0: ");

for (int i = 0; i < array.length; i++) {
    if (array[i] < 0) {
        System.out.print(array[i] + " ");
    } else if (array[i] > 0) {
        if (firstHalf){
            System.out.print("\nGreater than 0: ");
            firstHalf = false;
        }
        System.out.print(array[i] + " ");
    }

}
int[]数组={2,-5,4,12,54,-2,-50,150};
数组。排序(数组);
布尔值前半部分=真;
系统输出打印(“小于0:);
for(int i=0;i0){
如果(上半场){
系统输出打印(“\n大于0:”);
前半部分=假;
}
System.out.print(数组[i]+“”);
}
}

因为您对值进行了排序,所以您知道所有负值都在正值之前,所以您开始打印值,然后在遇到第一个正值时切换到新行

例如,如下所示,它还可以处理一个包含所有负值的数组、一个包含所有正值的数组,甚至一个空数组

这只使用您已经展示过的Java构造

int[] array = {2, -5, 4, 12, 54, -2, -50, 150};
Arrays.sort(array);
for (int i = 0, iFirstPositive = 0; i < array.length; i++) {
    if (array[i] < 0)
        iFirstPositive = i + 1; // Assume index of first positive value is next
    if (i == iFirstPositive) {
        if (i != 0)
            System.out.println(); // End line of negative values
        System.out.print("Greater than 0: "); // Start line of positive values
    } else if (i == 0) {
        System.out.print("Less than 0: "); // Start line of negative values
    } else {
        System.out.print(", ");
    }
    System.out.print(array[i]);
}
if (array.length != 0) {
    System.out.println(); // End line if anything printed
}

更简单,但优化程度稍低,您也可以使用两个循环:

int[] array = {2, -5, 4, 12, 54, -2, -50, 150};
Arrays.sort(array);
System.out.print("Less than 0:");
for (int i = 0; i < array.length; i++) {
    if (array[i] < 0) {
        System.out.print(" " + array[i]);
    }
}
System.out.println();
System.out.print("Greater than 0:");
for (int i = 0; i < array.length; i++) {
    if (array[i] > 0) {
        System.out.print(" " + array[i]);
    }
}
System.out.println();

这是流的完美用例:

System.out.println(Arrays.stream(array).filter(n -> n < 0).collect(Collectors.toList()));
System.out.println(Arrays.stream(array.filter)(n->n<0.collect(Collectors.toList());

我认为使用
分区(在Java8中引入)正是针对这种情况。您也不需要对数组进行排序

Map<Boolean,List<Integer>> map = IntStream.range(0,array.length)
                .mapToObj(i->array[i])
                .collect(Collectors.partitioningBy(a->a>0));
打印负数

map.get(false).forEach(integer -> System.out.print(integer+","));  
如果你想分类,你可以像贝娄一样

map.get(false).stream().sorted()....

考虑到问题的简单性,流的使用可能超出了当前的OP级别,因此这可能不是最好的答案。仍然有用,所以无论如何+1.;-)@安德烈亚斯说得很好。添加了另一个带有for each循环的示例。实际上,第二个解决方案的复杂性为
O(n)
,而排序数组的复杂性为
O(n*log n)
@JohannesKuhn两者都有相同的大O。我的“稍微不太优化”注释是指输出,而不是性能。第二个版本将始终打印两行,即使没有负值/正值。第一个版本只在适当的时候打印行,它用逗号分隔值,而第二个版本没有。在
O(n)
?@JohannesKuhn中对数组进行排序两种解决方案都排序(O(n*logn)),并且都有简单的循环(O(n))。就复杂度而言,它们是相同的(O(n*logn))。啊,对。但第二种解决方案不需要排序。
map.get(true).forEach(integer -> System.out.print(integer+","));  
map.get(false).forEach(integer -> System.out.print(integer+","));  
map.get(false).stream().sorted()....