gpt4 book ai didi

java - ArrayList不打印toString方法

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

所以,我有一个 ArrayList,并且有一个 toString 方法用于打印 ArrayList 中包含的字符串。

但是,每当我运行它时,我要么得不到任何响应,要么得到一些乱码的计算机语言。

我错过了什么,或者做错了什么?

import java.util.Scanner;
public class main
{
public static void main (String [] args)
{
while (1 == 1)
{
Scanner scan = new Scanner(System.in);

System.out.println("Enter a name.");
System.out.println();
System.out.print("--> ");
String namae = scan.nextLine();
String checkstop = namae;
checkstop = checkstop.toLowerCase();
String n1 = namae.substring(0, 1);
n1 = n1.toUpperCase();
String n2 = namae.substring(1, (namae.length()));
namae = n1+n2;

for (int i = 0; i < namae.length(); i++)
{
char checker = namae.charAt(i);
String checks = checker + "";
if (checks.equals(" "))
{
char change = namae.charAt(i + 1);
String changes = change + "";
changes = changes.toUpperCase();
String z1 = namae.substring(0, (i));
String z2 = change + (namae.substring((i + 2), (namae.length())));
namae = z1 + z2;
}
}

if (namae.length() < 3)
{
System.out.println("Invalid Input! Too few characters.");
}
else if (checkstop.equals("stop"))
{
break;
}
else
{
send(namae);
}
}
}

public static void send(String namae)
{
InsertionSort s = new InsertionSort(namae);
}
}

和构造函数类:

import java.util.ArrayList;
public class InsertionSort
{
String namae;

public InsertionSort(String namae)
{
this.namae = namae;
}

public void Sort()
{
ArrayList<String> list = new ArrayList<String>();
list.add(namae);

for (int i = 1; i < list.size(); i++)
{
int j = i;
String tmp = list.get(i).substring(0, 1);
for(j = i - 1; j >= 0; j--)
{
if ((tmp.compareTo(list.get(j).substring(0, 1))) > 0)
{
list.set(j ,tmp);
}
if ((tmp.compareTo(list.get(j).substring(0, 1))) == 0)
{
list.set(j ,tmp);
}
if ((tmp.compareTo(list.get(j).substring(0, 1))) < 0)
{
list.set(j ,tmp);
}
}
}

for (int g = 0; g < list.size(); g++)
{
toString(list.get(g));
}
}

public static String toString(String x)
{
System.out.println(x);
return x;
}
}

最佳答案

由于您的 Sort 方法从未被调用,因此将代码更改为

public static void send(String namae)
{
InsertionSort s = new InsertionSort(namae);
s.Sort ();
}

还请遵守 Java 编码标准,即方法应以小写字母开头

关于java - ArrayList不打印toString方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37871570/

25 4 0