gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:12:19 25 4
gpt4 key购买 nike

所以我已经编写了一个程序,它按照我希望的方式正确输出,但由于某种原因,我在程序末尾仍然收到一些错误,我不希望显示这些错误。我尝试过使用 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());
});
});

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

最佳答案

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

Collections.sort 会修改 animalGroup 的内容。您是在迭代 animalGroup 的过程中执行此操作的。如果在迭代列表时某些元素被交换,结果将是一团糟。因此,禁止在迭代列表时修改列表,因此会出现ConcurrentModificationException。这被称为 Java 中集合实现的快速失败策略,您可以阅读更多相关信息 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()));

关于java - 如何从程序的输出中删除错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40852163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com