问题描述
好的,这是作业。我尝试了几件事但却无法让它发挥作用。说明是 - 写一个程序,将30个随机字母插入一个List。执行以下操作并显示结果:
a)按升序对列表进行排序。
b)按降序对List进行排序。
c)按升序显示列表,删除重复项。我有最简单的部分,但是当涉及到排序我不知道从哪里开始。
公共类RandomCharacters {
//私有静态列表randomCharacters;
public static void main(String [] args){
//创建Char类型列表
List< Character> randomCharacters = new ArrayList<>();
// Character [] randChars = new Character [30];
//生成随机字母并添加到列表
for(int i = 0; i< 30; i ++){
int num =(int)(26 * Math 。随机());
// randChars [i] =(char)(num +'a');
char letter =(char)(num +'a');
randomCharacters.add(letter);
//System.out.println(letter);
}
//System.out.println(Unsorted list =+ Arrays.asList(randChars));
System.out.println(Unsorted list =+ randomCharacters);
System.out.println(升序排序:);
Arrays .stream(randomCharacters)
.sorted()
.collect(Collectors.toList());
}
我尝试过两种方式。一个使用Character Array并使用tolist转换它,只使用ArrayList但排序不起作用。关于为什么这不起作用的任何提示。
好的谢谢你们我使用了你们两个组合的组合,这里是我的完整代码。我觉得它对我需要的东西有好处,但是如果有人认为它可以更有效率让我知道
公共类RandomCharacters {
私有静态列表<字符> randomCharacters;
public static void main(String [] args){
//创建Char类型列表
List< Character> randomCharacters = new ArrayList<>();
//生成随机字母并添加到列表
for(int i = 0; i< 30; i ++){
int num =(int) (26 * Math.random());
char letter =(char)(num +'a');
randomCharacters.add(letter);
}
System.out.println(Unsorted list =+ randomCharacters);
列表<字符> sortedList = randomCharacters.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(升序排序:+ sortedList);
列表<字符> reversedList = randomCharacters.stream()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
System.out.println(Sorted Descending:+ reversedList);
列表<字符> uniqueList = randomCharacters.stream()
.sorted()
.distinct()
.collect(Collectors.toList());
System.out.println(Unique Sort:+ uniqueList);
}
}
我会在上交之前添加更多评论
ok this is homework. I have tried a couple things but just cant get it to work. the instructions are--Write a program that inserts 30 random lettersinto a List. Perform the following operations and display your results:a) Sort the List in ascending order.b) Sort the List in descending order.c) Display the List in ascending order with duplicates removed. I have the simplest part done but when it comes to the sorting im not sure where to start. public class RandomCharacters {
// private static List randomCharacters;
public static void main(String[] args) {
//Create List of Char type
List<Character> randomCharacters = new ArrayList<>();
//Character[] randChars = new Character[30];
//generate random letters and add to the list
for(int i = 0; i<30; i++){
int num = (int) (26* Math.random());
//randChars[i] = (char) (num + 'a');
char letter = (char) (num + 'a');
randomCharacters.add(letter);
//System.out.println(letter);
}
//System.out.println( "Unsorted list= " + Arrays.asList(randChars));
System.out.println( "Unsorted list= " + randomCharacters);
System.out.println("Ascending sort: ");
Arrays .stream(randomCharacters)
.sorted()
.collect(Collectors.toList());
}
I have tried doing it two ways. one with the Character Array and converting that using tolist and by just using the ArrayList but the sort is not working. any hints as to why this is not working.
alright thanks guys i used a combination of both of your soulutions and here is my complete code. I think its good for what i need but if anyone thinks it can be more efficent let me know public class RandomCharacters {
private static List<Character> randomCharacters;
public static void main(String[] args) {
//Create List of Char type
List<Character> randomCharacters = new ArrayList<>();
//generate random letters and add to the list
for(int i = 0; i<30; i++){
int num = (int) (26* Math.random());
char letter = (char) (num + 'a');
randomCharacters.add(letter);
}
System.out.println( "Unsorted list= " + randomCharacters);
List<Character> sortedList = randomCharacters.stream()
.sorted()
.collect(Collectors.toList());
System.out.println("Ascending sort: " + sortedList);
List<Character> reversedList = randomCharacters.stream()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
System.out.println("Sorted Descending: " + reversedList);
List<Character> uniqueList = randomCharacters.stream()
.sorted()
.distinct()
.collect(Collectors.toList());
System.out.println("Unique Sort: "+ uniqueList);
}
}
i will add more comments before turning in as well
这篇关于在java中使用lambdas对字符列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!