我编写了这个程序来读取扫描仪的输入,并使用 ArrayList 将其存储为对象。但是,当我测试它时,它没有打印正确的内容。就像我输入:Apple Pie,输出是 Words@4aa298b7,Words@7d4991ad。
我错过了什么吗?这是我的代码:
import java.util.ArrayList;
import java.util.Scanner;
public class W7E2 {
public static void main(String[]args) {
System.out.println("Please anter words: ");
Scanner sc = new Scanner(System.in);
String []w = sc.nextLine().split(" ");
ArrayList<Words> word = new ArrayList<Words>();
for(int i = 0; i < w.length; i++) {
word.add(new Words(w[i]));
}
System.out.println(word);
}
}
class Words {
private String wor;
public Words(String wor) {
this.wor = wor;
}
}