如何从程序的输出中删除错误?

时间:2016-11-28 19:38:17

标签: java compiler-errors

所以我已经编写了一个程序输出,因为我想要它正确但但由于某种原因我仍然在程序结束时遇到一些错误,我不想显示。我尝试过使用Systematize(out);,但所有这一切都是将错误的颜色从红色更改为与程序其余部分相同的颜色。以下是该计划的输出:

************Wolf"************
Name = Bob
Age = 2
Noise = Woof!
************Parrot************
Parrot's name = Polly
Age = 6
Noise = Argh!
************Exception examples************
java.lang.Exception: Carnivores only eat meat!
Exception caught above
Exception caught below
java.lang.Exception: Herbivores only eat plants!
************Herbivore non-caught Exception example************
Herbivores eat Vegetables
************Carnivore non-caught Exception example************
Carnivores eat Steak
************Omnivore eating habbit************
Omniivores eat meat or fruit and vegertables
************New herbivore animal************
Parrot's name = Haryy
Age = 2
Noise = Squeek!
************Animal being fed************
Wolf has eaten 10 times
************New wolf creation************
Name = newborn
Age = 0
************ArrayList before sorting************
Sam,5
************ArrayList after sorting************
Pat,10
Wesley,7
Sam,5
George,3
Wesley,7
************ArrayList after sorting************
Pat,10
Wesley,7
Sam,5
George,3
Sam,5
************ArrayList after sorting************
Pat,10
Wesley,7
Sam,5
George,3
Exception in thread "main" java.util.ConcurrentModificationException
George,3
************ArrayList after sorting************
Pat,10
Wesley,7
Sam,5
George,3
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1380)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEachOrdered(ReferencePipeline.java:423)
    at QuestionEditor.Main.main(Main.java:124)
C:\Users\lee-pc\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

以下是输出包含错误的部分的主要方法:

   Demo.main(args); // populate the ArrayList in Demo Class main method 2.
    ArrayList<Animal> animalGroup = Demo.animalGroup; // Retrieve the ArrayList in Demo class.
    animalGroup.stream().map((animal) -> {
        System.out.println(animal.getName()+","+animal.getAge());
            return animal;
        }).map((Animal _item) -> {
            System.out.println("************ArrayList after sorting************");
            return _item;
        }).map((Animal _item) -> {
            Collections.sort(animalGroup,new AgeComparator()); // Sort highest to lowest
            return _item;
        }).forEachOrdered((Animal _item) -> {
            animalGroup.forEach((animal2) -> {
                System.out.println(animal2.getName() + "," + animal2.getAge());
            });
        });

有人知道为什么会这样吗?解决这个问题的最佳方法是什么?感谢您的任何反馈,非常感谢。

1 个答案:

答案 0 :(得分:2)

如果有任何元素不按排序顺序,则

Collections.sort会修改animalGroup的内容。 您正在迭代animalGroup的过程中执行此操作。 如果在迭代列表时某些元素被交换, 结果将是一团糟。 因此,禁止在迭代时修改列表, 因此ConcurrentModificationException。 这被称为Java中集合实现的 fail-fast 策略, 你可以阅读更多相关信息here

你对溪流所做的事情并没有多大意义。 看来你正在寻找这个:

List<Animal> animalGroup = Demo.animalGroup; 

animalGroup.forEach(animal -> System.out.println(animal.getName() + "," + animal.getAge()));

System.out.println("************ArrayList after sorting************");

Collections.sort(animalGroup, new AgeComparator()); // Sort highest to lowest

animalGroup.forEach(animal -> System.out.println(animal.getName() + "," + animal.getAge()));