ArrayList不打印toString方法

时间:2016-06-17 01:02:56

标签: java arraylist constructor bluej

所以,我有一个ArryList,并且有一个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;
}
}

3 个答案:

答案 0 :(得分:1)

由于您永远不会调用Sort方法,因此请将代码更改为

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

另外请坚持使用java编码标准,即方法应以小写

开头

答案 1 :(得分:1)

此外,您永远不会在程序中调用Sort()方法。

InsertionSort s = new InsertionSort(namae);
s.Sort(); // Perform sorting.

答案 2 :(得分:0)

只需遍历ArrayList并打印每个元素:

for (String str : list) {
    System.out.println(str);
}