ArrayList的正数和负数

时间:2016-05-14 22:14:50

标签: java arraylist input joptionpane

如何分析正数和负数?我有这段代码:

<form action='/' method='POST'>

假设我要添加5个数字。 import java.util.ArrayList; import java.swing.JOptionPane; public class Test { public static void main(String[] args) { ArrayList al = new ArrayList(); int num=Integer.parseInt(JOptionPane.showInputDialog("How many numbers?")); for (int i=0 ; i < num ; i++) { al.add(Integer.parseInt(JOptionPane.showInputDialog("What numbers?"))); } } }

我需要输出为-2, -7, 8, 4, -1Positive = 8, 4

3 个答案:

答案 0 :(得分:2)

如果您使用Java 8,则可以使用流轻松分离它们:

List<Integer> positive = new ArrayList<>();
List<Integer> negative = new ArrayList<>();
al.stream().forEach(i -> (i < 0 ? negative : positive).add(i));
System.out.println(positive);
System.out.println(negative);

您有两个列表,其中一个包含正数,另一个包含负数。

答案 1 :(得分:0)

如果你想要的只是数组的输出,那么一次循环通过数组就可以轻松快速地工作,即使对于大型数组也是如此。有了它,您可以使用StringBuilder对象来处理输出。

StringBuilder pos = new StringBuilder();
StringBuilder neg = new StringBuilder();

pos.append("Positive = ");
neg.append("Negative = ");

//note that 0 will be considered positive
for (int i = 0; i < al.size(); i++) {
    if (al.get(i) >= 0) {
        pos.append(al.get(i) + ", ");
    } else {
        neg.append(al.get(i) + ", ");
    }
}

//handles the extra "," at end of strings
pos.deleteCharAt(pos.length() - 2);
neg.deleteCharAt(neg.length() - 2);

System.out.println(pos.toString() + " and " + neg.toString());

如果您想做比简单输出更复杂的事情,我建议您使用if逻辑将单个整数分配给两个单独的数组。

另外请注意,您应该告诉ArrayList期望的对象类型:

ArrayList<int> al = new ArrayList<>();

答案 2 :(得分:0)

解决您的问题:

public class Test {

    public static void printValues(ArrayList<Integer> list)
    {
        StringBuilder positive = new StringBuilder("Positive = ");
        StringBuilder negative = new StringBuilder("Negative = ");
        for (Integer i : list)
        {
            if (i >= 0)
            {
                positive.append(i).append(", ");
            }
            else
            {
                negative.append(i).append(", ");
            }
        }
        positive.delete(positive.length() - 2, positive.length());
        negative.delete(negative.length() - 2, negative.length());
        System.out.println(positive.toString());
        System.out.println(negative.toString());
    }

    public static void main(String[] args) {
        ArrayList al = new ArrayList();

        int num=Integer.parseInt(JOptionPane.showInputDialog("How many numbers?"));
        for (int i=0 ; i < num ; i++) {
            al.add(Integer.parseInt(JOptionPane.showInputDialog("What numbers?")));       
        }
        printValues(al);
    }
}

或者您可以使用lambda表达式和流

public class Test {

    public static void printValues(ArrayList<Integer> list)
    {
        StringBuilder positive = new StringBuilder("Positive = ");
        StringBuilder negative = new StringBuilder("Negative = ");

        list.stream().forEach(i -> (i < 0 ? negative : positive).append(i).append(", "));

        positive.delete(positive.length() - 2, positive.length());
        negative.delete(negative.length() - 2, negative.length());
        System.out.println(positive.toString());
        System.out.println(negative.toString());
    }

    public static void main(String[] args) {
        ArrayList al = new ArrayList();

        int num=Integer.parseInt(JOptionPane.showInputDialog("How many numbers?"));
        for (int i=0 ; i < num ; i++) {
            al.add(Integer.parseInt(JOptionPane.showInputDialog("What numbers?")));
        }
        printValues(al);
    }
}