打印并访问List< String []> [英] Print and access List <String[]>
本文介绍了打印并访问List< String []>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在读取文件并将其存储在t1中。如何访问t1中的元素?当我尝试打印它时,我得到地址而不是值。还有 String
和 String []
之间的区别?
I'm reading in a file and storing it in t1. How do I access the elements in t1? When I try to print it I get addresses instead of values. Also whats the difference between String
and String[]
?
CSVReader reader = new CSVReader(new FileReader("src/new_acquisitions.csv"));
List <String[]> t1 = reader.readAll();
int i = 0
while(i < t1.size()) {
System.out.println(t1.get(i));
i++;
}
输出:
[Ljava.lang.String;@9304b1
[Ljava.lang.String;@190d11
[Ljava.lang.String;@a90653
[Ljava.lang.String;@de6ced
推荐答案
String []是一个字符串数组,因此它没有像你期望的那样打印,请尝试:
String[] is an array of strings, hence the reason it is not printing as you would expect, try:
for (int i = 0; i < t1.size(); i++) {
String[] strings = t1.get(i);
for (int j = 0; j < strings.length; j++) {
System.out.print(strings[j] + " ");
}
System.out.println();
}
或更简洁:
for (String[] strings : t1) {
for (String s : strings) {
System.out.print(s + " ");
}
System.out.println();
}
或更好:
for (String[] strings : t1) {
System.out.println(Arrays.toString(strings));
}
这篇关于打印并访问List< String []>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文