如何通过toArray方法显示ArrayList的元素转换为Object数组? [重复]

时间:2022-09-11 12:06:43

This question already has an answer here:

这个问题在这里已有答案:

How to show element of ArrayList converted to Object array by toArray methode?

如何通过toArray方法显示ArrayList的元素转换为Object数组?

I want to show array a's element (code in below) but It seem to just return address of the array. please help me ...T^T

我想显示数组a的元素(下面的代码),但它似乎只返回数组的地址。请帮帮我...... T ^ T.

import java.util.ArrayList;
import java.util.Arrays;


public class testworld {

    static ArrayList<history> histories =new ArrayList<history>();

    public static void main(String[] args) {    

    history m1 =new history();
    m1.setAmount(""+0);
    m1.setUse("use");
    m1.setDate("date");
    m1.setTime("time");

    history m2 =new history();
    m2.setAmount(""+0);
    m2.setUse("use2");
    m2.setDate("date2");
    m2.setTime("time2");

    history m3 =new history();
    m3.setAmount(""+0);
    m3.setUse("use3");
    m3.setDate("date3");
    m3.setTime("time3");


    histories.add(0,m1);
    histories.add(1,m2);
    histories.add(2,m3);

    Object[] a = histories.toArray();

    System.out.println(Arrays.toString(a));
    System.out.println(a[0]);
    System.out.println(a[1]);
    System.out.println(a[2]);

    System.out.print(a);
    System.out.println(a);

    }
}

[notepad.history@15db9742, notepad.history@6d06d69c, notepad.history@7852e922]

[notepad.history@15db9742,notepad.history @ 6d06d69c,notepad.history @ 7852e922]

1 个解决方案

#1


1  

What you see there is the content of the array. You must overwrite the toString() method in the history class to see some useful information. The code you are asking for is

你看到的是阵列的内容。您必须覆盖历史记录类中的toString()方法才能看到一些有用的信息。您要求的代码是

history[] a = new history[histories.size()];
a = histories.toArray(a);

I understand you are a beginner at java. Please read the javadocs or do some research before posting any further questions on *. Also, read the java naming conventions. It 's History not history. Always use camel-case.

我知道你是java的初学者。在发布有关*的任何进一步问题之前,请阅读javadocs或进行一些研究。另外,请阅读java命名约定。历史不是历史。始终使用驼峰式表壳。

#1


1  

What you see there is the content of the array. You must overwrite the toString() method in the history class to see some useful information. The code you are asking for is

你看到的是阵列的内容。您必须覆盖历史记录类中的toString()方法才能看到一些有用的信息。您要求的代码是

history[] a = new history[histories.size()];
a = histories.toArray(a);

I understand you are a beginner at java. Please read the javadocs or do some research before posting any further questions on *. Also, read the java naming conventions. It 's History not history. Always use camel-case.

我知道你是java的初学者。在发布有关*的任何进一步问题之前,请阅读javadocs或进行一些研究。另外,请阅读java命名约定。历史不是历史。始终使用驼峰式表壳。