Java 打印和访问列表 <String[]>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2983577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:15:41  来源:igfitidea点击:

Print and access List <String[]>

javastringlist

提问by nubme

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 Stringand String[]?

我正在读取一个文件并将其存储在 t1 中。如何访问 t1 中的元素?当我尝试打印它时,我得到的是地址而不是值。还有String和之间有什么区别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++;
        }

output:

输出:

[Ljava.lang.String;@9304b1
[Ljava.lang.String;@190d11
[Ljava.lang.String;@a90653
[Ljava.lang.String;@de6ced

采纳答案by DaveJohnston

String[] is an array of strings, hence the reason it is not printing as you would expect, try:

String[] 是一个字符串数组,因此它没有按预期打印的原因,请尝试:

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();
}

Or more concise:

或者更简洁:

for (String[] strings : t1) {
    for (String s : strings) {
        System.out.print(s + " ");
    }
    System.out.println();
}

Or better yet:

或者更好:

for (String[] strings : t1) {
    System.out.println(Arrays.toString(strings));
}

回答by Petar Minchev

String[]is an array of strings. You print the address of the array - when you see [Lthat means an array. You have to iterate through the elements of every array, instead of printing the address:

String[]是一个字符串数组。你打印数组的地址 - 当你看到[L这意味着一个数组。您必须遍历每个数组的元素,而不是打印地址:

String[] temp = t1.get(i);
for (int j = 0; j < temp.length; j++)
    System.out.println(temp[j]);

回答by RedPandaCurios

As Petar mentioned, your list is a List of Arrays of Strings, so you are printing out the array, not the array contents.

正如 Petar 所提到的,您的列表是一个字符串数组列表,因此您打印的是数组,而不是数组内容。

A lazy way to print out the array contents is to convert the array to a List<String>with java.utils.Arrays.toString():

打印出数组内容的一种懒惰方法是将数组转换为List<String>with java.utils.Arrays.toString()

String[] stringArray=new String[] { "hello", world };
System.out.println(Arrays.toString(stringArray));

gives

["hello","world"]

[“你好,世界”]

回答by josefx

You print a List with arrays. While the List classes overload the toString() method to print each element, the array uses the default toString used by Object which only prints the classname and the identity hash.

您打印带有数组的列表。虽然 List 类重载 toString() 方法来打印每个元素,但数组使用 Object 使用的默认 toString ,它只打印类名和身份哈希。

To print all you either have to iterate through the List and print each array with Arrays.toString().

要打印所有内容,您必须遍历 List 并使用 Arrays.toString() 打印每个数组。

for(String[] ar:t1)System.out.print("["+Arrays.toString(ar)+"]");

Or you put each array into a List

或者您将每个数组放入一个列表中

List<List<String>> tt1 = new ArrayList<List<String>>();
for(String[] ar: t1)tt1.add(Arrays.asList(ar));//wraps the arrays in constant length lists
System.out.println(tt1)

回答by Marc

As I said in your other question, when creating your own Record object, you can implement the toString()function for returning a reasonable representation of that piece of information.

正如我在您的其他问题中所说,在创建您自己的 Record 对象时,您可以实现toString()返回该信息的合理表示的函数。

回答by Ankit

You can print the list content by simply converting the list in to array first and then using Arrays.toString like below

您可以通过简单地首先将列表转换为数组然后使用 Arrays.toString 来打印列表内容,如下所示

System.out.println(Arrays.toString(your_list.toArray());