简体   繁体   English

将java.lang.object转换为Integer时出现问题

[英]Issue converting java.lang.object to Integer

Hi am developping a java web project with spring mvc and hibernate. 嗨,我正在用Spring MVC和Hibernate开发一个Java Web项目。 I have a list of cars in carList. 我在carList中有汽车清单。 Now I want to retrieve the car type of each car. 现在,我想检索每辆汽车的汽车类型。 So I initialise another list carType. 因此,我初始化了另一个列表carType。 and then loop into carList and append the car type in the list of carType. 然后循环进入carList并将汽车类型附加在carType列表中。 When i output the value store in carType, it gives [Ljava.lang.Object;@255a8ce4. 当我在carType中输出值存储时,它给出[Ljava.lang.Object; @ 255a8ce4。 Can someone help me? 有人能帮我吗? The type of list should be object. 列表的类型应该是对象。 Because I am sending this list to birt reporting to display in pdf. 因为我将此列表发送到Birt报告中,以pdf显示。

List<Object> carList=getCarService().retrieveAllCarsAssignedToManagers(CarNo, DeptNo);

    List<Object> carType = new ArrayList<Object> ();
    for (Object carLists:carList) {
         carType.add(getcarService().retrieveAllCarType(Integer.valueOf(carLists.toString())));

    }
    for (int i = 0;i < carType.size(); i++) {
        System.out.println("Cars:  " + carType.get(i));
    }

You get Ljava.lang.Object;@255a8ce4. 您得到Ljava.lang.Object;@255a8ce4. because your method retrieveAllCarType returns an array or list and not what you are expecting. 因为您的方法retrieveAllCarType返回一个数组或列表,而不是您所期望的。 When you put your list into System.out.print then it calls toString method of that list internally. 当您将列表放入System.out.print时,它将在内部调用该列表的toString方法。 The class of the list doesn't implement toString method so you get the default toString output. 列表的类未实现toString方法,因此您将获得默认的toString输出。

Try System.out.println(java.util.Arrays.toString(carType.get(i).toArray())); 尝试System.out.println(java.util.Arrays.toString(carType.get(i).toArray()));

1) You define the List<Object> carType , the object here means the list can contain everything, including collections(such as List , Map and so on). 1)您定义List<Object> carType ,这里的对象表示列表可以包含所有内容,包括集合(如ListMap等)。

According to your code, you should put the integer cartype to the list, but you put a List<Object> to List<Object> , then elements in List carType is a List<Object> . 根据您的代码,您应该将整数cartype放到列表中,但是将List<Object>放到List<Object> ,那么List carType中的元素就是List<Object> so when you call 所以当你打电话

carType.get(i)

it returns a List! 它返回一个列表!

If you can confirm that the carType is a number, you can define the carType as List<Integer> , then you can never put a List to carType, only Intergers are allowed in carType. 如果可以确认carType是数字,则可以将carType定义为List<Integer> ,则永远不能将List放在carType上,在carType中只允许使用Interger。

2)sysout one object will call his default toString() , your output is the default toString() of List type. 2)sysout一个对象将调用其默认的toString() ,您的输出是List类型的默认toString() It output the classname and it's hashcode. 它输出类名及其哈希码。

You are providing too few information to get exact issue in your code. 您提供的信息太少,无法在代码中得到确切的问题。 But I analyzed it by undergoing you code and question. 但是我通过接受代码和问题来分析了它。 There is one mistake done at 在这里犯了一个错误

Integer.valueOf(carLists.toString()).

A list holds any type of information, like any type of object (String, List, Map, Set) and you are trying to convert that list into an Integer. 列表包含任何类型的信息,例如任何类型的对象(String, List, Map, Set)而您正在尝试将该列表转换为Integer。 You should try removing Integer.valueOf() and check what the list contains. 您应该尝试删除Integer.valueOf()并检查列表中包含的内容。

thanks for you help. 谢谢你的帮助。 I really apreaciate. 我真的很感激 Manage to solve the issue using addAll instead of add. 使用addAll而不是add设法解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM