英文:
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;@7852e922
是 Object 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("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]));
}
}
And the result is :
Luke 11
Luke2 12
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论