如何反转在​​其他类中创建的ImageIcon数组的内容?

我目前有一个名为ImageShow的ArrayList。我一直在尝试寻找在Java中交换该数组内容的方法,但我还没有找到方法。

ImageShow.add(new ImageIcon(file.listFiles()[a].getabsolutePath())); //Adding the files into an imageicon array

这就是我将文件内容放入数组的方式,然后我希望它能够反转数组中所有项目的内容。例如,该数组将依次包含15.png,23.png,32.png,47.png,58.png,64.png,76.png,81.png,但是我希望能够按一个按钮,它会反转它。有人可以帮忙吗?我已经找到了使用字符串而不是ImageIcon数组的方法。

谢谢。

aappww 回答:如何反转在​​其他类中创建的ImageIcon数组的内容?

使用stream是解决此问题的简便方法。将单个arg传递给ImageIcon时,arg既是文件名又是描述。

final Comparator<ImageIcon> sortBy = Comparator.comparing(ImageIcon::getDescription).reversed();
List<ImageIcon> sortedList = ImageShow.stream().sorted(sortBy).collect(toList());
本文链接:https://www.f2er.com/3130749.html

大家都在问