如何以数组形式获取响应

huangapple go评论54阅读模式
英文:

How to get Response in form of an Array

问题

当我打印时,它显示为[Ljava.lang.Object;@7852e922

我需要以Employee数组的形式得到响应,请问如何以数组的形式获取这个响应?

英文:

Code

package com;

public class Employee {

	private String name;

	private String empId;

	public Employee(String name, String empId) {
		super();
		this.name = name;
		this.empId = empId;
	}

	// equals and hashcode

}


package com;

import java.util.LinkedHashSet;
import java.util.Set;

public class TestEmp {

	public static void main(String[] args) {
		Employee emp1 = new Employee("Luke", "11");
		Employee emp2 = new Employee("Luke2", "12");

		Set<Employee> empSet = new LinkedHashSet<>();

		empSet.add(emp1);
		empSet.add(emp2);

		Object obj[] = empSet.toArray();

		System.out.println(obj);
	}

}

When i print i get the response it is showing as [Ljava.lang.Object;@7852e922

I need response in form of an Employee Array , can you please help how to get this in form of an array

The response needs to form of an Array of Employees

答案1

得分: 1

Employee类中,您需要实现一个toString方法:

@Override
public String toString() {
    return "Employee{" +
            "name='" + name + '\'' +
            ", empId='" + empId + '\'' +
            '}';
}

主要方法中的输出结果会是:

[Employee{name='Luke', empId='11'}, Employee{name='Luke2', empId='12'}]
英文:

You would need to implement a toString method in the Employee class

public static class Employee {

        private String name;


        private String empId;

        public Employee(String name, String empId) {
            super();
            this.name = name;
            this.empId = empId;
        }

        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", empId='" + empId + '\'' +
                    '}';
        }
    }

Main

public static void main(String[] args){
        Employee emp1 = new Employee("Luke","11");
        Employee emp2 = new Employee("Luke2","12");

        Set<Employee> empSet = new LinkedHashSet<>();

        empSet.add(emp1);
        empSet.add(emp2);
        System.out.println(empSet);


    }

Output:

[Employee{name='Luke', empId='11'}, Employee{name='Luke2', empId='12'}]

答案2

得分: 1

在Employee类中覆盖toString方法:

```java
@Override
public String toString() {
    return "Employee{" +
            "name='" + name + '\'' +
            ", empId='" + empId + '\'' +
            '}';
}

在TestEmp类中使用Arrays.toString进行打印:

System.out.println(Arrays.toString(obj));
英文:

Override toString method in Employee class :

@Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", empId='" + empId + '\'' +
                '}';
    }

Use Arrays.toString to print in TestEmp class:

    System.out.println(Arrays.toString(obj));

答案3

得分: 0

java.lang.Object;@7852e922Object obj[] = empSet.toArray() 的对象地址。

我给你一个关于使用反射从 Object 类获取值并将其存储到二维数组中的想法。

我的想法不需要重写 toString() 方法,而是真正将值存储到数组中。

我认为 empArray 就是你想要的。


import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

public class TestEmp {
    public static void main(String[] args) throws IllegalAccessException {
        // TODO Auto-generated method stub

        Employee emp1 = new Employee("Luke", "11");
        Employee emp2 = new Employee("Luke2", "12");

        Set<Employee> empSet = new LinkedHashSet<>();

        empSet.add(emp1);
        empSet.add(emp2);


        Object obj[] = empSet.toArray();

        Field[] fs = Employee.class.getDeclaredFields();
        String [][] empArray = new String[obj.length][fs.length];

        for(int i=0; i<obj.length; i++){
            Class a = (Class) obj[i].getClass();
            for(int j=0; j<fs.length; j++){
                Field f = fs[j];
                f.setAccessible(true);
                Object val = f.get(obj[i]);
                empArray[i][j] = (String) val;
            }
        }
        Arrays.asList(empArray).forEach(x -> System.out.println(x[0]+" "+x[1]));
    }
}

结果是:

Luke 11
Luke2 12
英文:

java.lang.Object;@7852e922 is the Object address of Object obj[] = empSet.toArray()

I give you an idea about using reflect to get value from Object Class and then store it into 2D arrays.

My idea does not need to overwrite toString() method but truely store value into an arrays.

I think empArray it is what you want.


import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

public class TestEmp {
    public static void main(String[] args) throws IllegalAccessException {
        // TODO Auto-generated method stub

        Employee emp1 = new Employee(&quot;Luke&quot;, &quot;11&quot;);
        Employee emp2 = new Employee(&quot;Luke2&quot;, &quot;12&quot;);

        Set&lt;Employee&gt; empSet = new LinkedHashSet&lt;&gt;();

        empSet.add(emp1);
        empSet.add(emp2);


        Object obj[] = empSet.toArray();

        Field[] fs = Employee.class.getDeclaredFields();
        String [][] empArray = new String[obj.length][fs.length];

        for(int i=0; i&lt;obj.length; i++){
            Class a = (Class) obj[i].getClass();
            for(int j=0; j&lt;fs.length; j++){
                Field f = fs[j];
                f.setAccessible(true);
                Object val = f.get(obj[i]);
                empArray[i][j] = (String) val;
            }
        }
        Arrays.asList(empArray).forEach(x -&gt; System.out.println(x[0]+&quot; &quot;+x[1]));
    }
}

And the result is :

Luke 11
Luke2 12

huangapple
  • 本文由 发表于 2020年7月22日 13:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定