[英]Sorting array of names alphabetically using compareTo()
I am trying to sort an array of names alphabetically by using compareTo()
and a method String addSort(String name)
but I get an error when compiling at the line "return name", saying that my "variable "name" may not be initialized" when it already is. 我正在尝试使用compareTo()
和方法String addSort(String name)
按字母顺序对名称数组进行排序,但是在“返回名称”行进行编译时出现错误,并说我的“变量”名称可能不是已初始化”。
I've made the method and the code for sorting alphabetically which I think should be correct when using compareTo() as a way of sorting the array. 我已经制作了按字母顺序排序的方法和代码,在使用compareTo()作为对数组进行排序的方式时,我认为应该是正确的。 (The array "moreFriends" is a new array which doubles the size of the original array "friends" when it gets full) (Note this is not all of the code) (数组“ moreFriends”是一个新的数组,当数组变满时,它的大小是原始数组“ friends”的两倍)(请注意,这并不是全部代码)
public class SimpleDataStructure{
private String [] friends;
private String [] moreFriends;
private int counter;
public SimpleDataStructure()
{
friends= new String[5];
counter=0;
}
public String addSort(){
String name;
for(int i = 0; i < moreFriends.length; i++){
for(int j = i + 1; j < moreFriends.length; j++){
if(moreFriends[i].compareTo(moreFriends[j]) > 0){
String temp = moreFriends[i];
moreFriends[i] = moreFriends[j];
moreFriends[j] = temp;
}
}
}
System.out.println("Names in sorted order:");
for(int i = 0; i < moreFriends.length -1; i++){
System.out.println(moreFriends[i]);
}
return name;
}
public static void main( String [] arg){
SimpleDataStructure sortedfriends = new SimpleDataStructure();
System.out.println(sortedfriends.addSort(name));
}
This is the error message I get when i try to compile the program: 这是我尝试编译程序时收到的错误消息:
SimpleDataStructure.java:85: error: variable name might not have been initialized
return name;
^
1 error
When I expect the output to eventually be: 当我期望输出最终是:
(unsorted)
Kalle
Bob
Carl
Alice
Lewis
(sorted)
Alice
Bob
Carl
Kalle
Lewis
You need declare youre function like this: 您需要这样声明您的功能:
public String addSort(String name){
and delete the string declaration: 并删除字符串声明:
String name;
and you don't put value for name. 并且您不重视价值。
You can solved your problem using this: 您可以使用以下方法解决您的问题:
String [] a="a","v","b";
Arrays.sort(a);
The reason that you are getting the compile error is because you never set a value to the String name
before trying to use it. 出现编译错误的原因是,在尝试使用它之前,您从未为String name
设置任何值。
You should be passing in the value like you have in your description addSort(String name)
. 您应该像在描述addSort(String name)
那样传递值。 This will remove that error. 这将消除该错误。
I do not see a reason why you are returning the String
in your function. 我看不到为什么要在函数中返回String
的原因。 This function does not appear to add the passed in name either. 该函数似乎也没有添加传入的名称。
Hope this helps! 希望这可以帮助!
import java.util.*;
class SimpleDataStructure {
public String[] addSort(String moreFriends []) {
for (int i = 0; i < moreFriends.length; i++) {
for (int j = i + 1; j < moreFriends.length; j++) {
if (moreFriends[i].compareTo(moreFriends[j]) > 0) {
String temp = moreFriends[i];
moreFriends[i] = moreFriends[j];
moreFriends[j] = temp;
}
}
}
return moreFriends;
}
public static void main(String[] arg) {
Scanner input=new Scanner(System.in);
SimpleDataStructure sortedFriends = new SimpleDataStructure();
String [] name =new String[5];
System.out.println("Enter name(s): ");
for (int i = 0; i < name.length; i++) {
name[i]=input.nextLine();
}
System.out.println("Unsorted:");
System.out.println(Arrays.toString(name));
System.out.println("Sorted:");
System.out.println(Arrays.toString(sortedFriends.addSort(name)));
}
} }
And if you want the names to print out line by line, just create a for loop instead of Arrays.toString 而且,如果您希望名称逐行打印出来,只需创建一个for循环而不是 Arrays.toString
Or you could even use Arrays.sort , which is much simpler 或者甚至可以使用Arrays.sort ,这要简单得多
import java.util.Arrays;
class SimpleDataStructure {
public String[] addSort(String moreFriends []) {
for (int i = 0; i < moreFriends.length; i++)
Arrays.sort(moreFriends);
return moreFriends;
}
public static void main(String[] arg) {
SimpleDataStructure sortedFriends = new SimpleDataStructure();
String [] name ={"Kalle", "Bob","Carl","Alice", "Lewis"};
System.out.println("Unsorted:");
System.out.println(Arrays.toString(name));
System.out.println("Sorted:");
System.out.println(Arrays.toString(sortedFriends.addSort(name)));
}
} }
I changed my arrays "friends" and "moreFriends" to static in my class. 我在课堂上将数组“ friends”和“ moreFriends”更改为static。 Now my code looks something like this when im calling my method in my main: 现在我在主程序中调用我的方法时,我的代码如下所示:
SimpleDataStructure sorted = new SimpleDataStructure();
System.out.println("Sorted:");
System.out.println(Arrays.toString(sorted.addSort()));
And this is my method: 这是我的方法:
public String[] addSort() {
for (int i = 0; i < moreFriends.length; i++) {
for (int j = i + 1; j < moreFriends.length; j++) {
if (moreFriends[i].compareTo(moreFriends[j]) > 0) {
String temp = moreFriends[i];
moreFriends[i] = moreFriends[j];
moreFriends[j] = temp;
}
}
}
return moreFriends;
}
However i get this error message now: 但是我现在收到此错误消息:
Unsorted:
Kalle Bob Carl Alice Lewis
Sorted:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.compareTo(Unknown Source)
at SimpleDataStructure.addSort(SimpleDataStructure.java:75)
at SimpleDataStructure.main(SimpleDataStructure.java:108)
You need to include your variable name, that holds the names, inside addSort 您需要在addSort中包含变量名称,该名称用于保存名称
SimpleDataStructure sorted = new SimpleDataStructure();
System.out.println("Sorted:");
System.out.println(Arrays.toString(sorted.addSort(**HERE**)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.