Java 使用compareTo()按字母顺序对名称数组进行排序
我试图使用Java 使用compareTo()按字母顺序对名称数组进行排序,java,Java,我试图使用compareTo()和方法String addSort(String name)按字母顺序对名称数组进行排序,但在“return name”行编译时出现错误,表示“variable”name“可能未初始化”,而它已经初始化 我制作了按字母顺序排序的方法和代码,我认为使用compareTo()对数组进行排序时应该是正确的。(数组“moreFriends”是一个新的数组,当它满的时候,它的大小是原始数组“friends”的两倍)(注意这不是全部代码) 这是我试图编译程序时收到的错误消息:
compareTo()
和方法String addSort(String name)
按字母顺序对名称数组进行排序,但在“return name”行编译时出现错误,表示“variable”name“可能未初始化”,而它已经初始化
我制作了按字母顺序排序的方法和代码,我认为使用compareTo()对数组进行排序时应该是正确的。(数组“moreFriends”是一个新的数组,当它满的时候,它的大小是原始数组“friends”的两倍)(注意这不是全部代码)
这是我试图编译程序时收到的错误消息:
SimpleDataStructure.java:85: error: variable name might not have been initialized
return name;
^
1 error
当我预计最终的结果是:
(unsorted)
Kalle
Bob
Carl
Alice
Lewis
(sorted)
Alice
Bob
Carl
Kalle
Lewis
您需要这样声明您的函数:
public String addSort(String name){
并删除字符串声明:
String name;
而且你不需要为名字赋值
您可以使用以下方法解决问题:
String [] a="a","v","b";
Arrays.sort(a);
您需要这样声明您的函数:
public String addSort(String name){
并删除字符串声明:
String name;
而且你不需要为名字赋值
您可以使用以下方法解决问题:
String [] a="a","v","b";
Arrays.sort(a);
出现编译错误的原因是,在尝试使用
字符串名称之前,从未将其设置为值
您应该像在描述中一样传递值addSort(字符串名称)
。这将删除该错误
我看不出您在函数中返回字符串的原因。
此函数似乎也不会添加传入的名称。出现编译错误的原因是,在尝试使用字符串名称之前,从未为其设置值
您应该像在描述中一样传递值addSort(字符串名称)
。这将删除该错误
我看不出您在函数中返回字符串的原因。
此函数似乎也不会添加传入的名称。希望这有帮助强>
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)));
}
import java.util.*;
类SimpleDataStructure{
公共字符串[]addSort(字符串moreFriends[]){
for(int i=0;i0{
字符串temp=moreFriends[i];
moreFriends[i]=moreFriends[j];
moreFriends[j]=临时工;
}
}
}
回报更多的朋友;
}
公共静态void main(字符串[]arg){
扫描仪输入=新扫描仪(System.in);
SimpleDataStructure sortedFriends=新SimpleDataStructure();
字符串[]名称=新字符串[5];
System.out.println(“输入名称):”;
for(int i=0;i
}
如果要逐行打印名称,只需创建for循环,而不是数组
或者您甚至可以使用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)));
}
导入java.util.array;
类SimpleDataStructure{
公共字符串[]addSort(字符串moreFriends[]){
for(int i=0;i
}希望这有帮助强>
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)));
}
import java.util.*;
类SimpleDataStructure{
公共字符串[]addSort(字符串moreFriends[]){
for(int i=0;i0{
字符串temp=moreFriends[i];
moreFriends[i]=moreFriends[j];
moreFriends[j]=临时工;
}
}
}
回报更多的朋友;
}
公共静态void main(字符串[]arg){
扫描仪输入=新扫描仪(System.in);
SimpleDataStructure sortedFriends=新SimpleDataStructure();
字符串[]名称=新字符串[5];
System.out.println(“输入名称):”;
for(int i=0;i
}
如果要逐行打印名称,只需创建for循环,而不是数组
或者您甚至可以使用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)));
}
导入java.util.array;
类SimpleDataStructure{
公共字符串[]addSort(字符串moreFriends[]){
for(int i=0;i
}我在课堂上将数组“friends”和“moreFriends”更改为静态。
现在,当我在main中调用我的方法时,我的代码如下所示:
SimpleDataStructure sorted = new SimpleDataStructure();
System.out.println("Sorted:");
System.out.println(Arrays.toString(sorted.addSort()));
这是我的方法:
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;
}
我在课堂上将数组“friends”和“moreFriends”更改为静态。
现在,当我在中调用我的方法时,我的代码看起来像这样