简体   繁体   English

对象列表。 使用整数条目的操作

[英]List of objects. Operations with Integer entries

The List might contain both Integers and String values. List可能包含Integers和String值。 In this case, should I create the List of Objects, right? 在这种情况下,我应该创建对象列表吗?

List<Object> list = new ArrayList<Object>();

How to perform simple arithmetic operations with the Integer entries of the List? 如何使用List的Integer条目执行简单的算术运算?

list.add(1);
list.add("ok");
list.add(2);
Integer a = list.get(0) - list.get(2); // does not work

您需要将Object为int,因为- Object上没有定义-运算符,Java也不会自动取消这些。

Integer a = ((Integer)list.get(0)) - ((Integer)list.get(2));

That's because ultimately, list.get(0); 那是因为最终, list.get(0); is an Object . 是一个Object You have to cast it if you want to do arithmetic operations on it: 如果你想对它进行算术运算,你必须抛出它:

Integer a = (Integer) list.get(0) - (Integer) list.get(2);

This is a really bad design to be honest. 说实话,这是一个非常糟糕的设计。 What if you want to iterate over that list? 如果你想迭代那个列表怎么办? You will have to manually check if the element is a string or an integer. 您必须手动检查元素是字符串还是整数。 Generics where introduced to Java for a reason. 出于某种原因引入Java的泛型。

Can't you make 2 lists: one for strings and one for integers. 你不能制作2个列表:一个用于字符串,一个用于整数。 Or at least use one list but instead of using strings use a (normally unused) integer value? 或者至少使用一个列表但不使用字符串使用(通常未使用的)整数值?

Jackson (Java):反序列化相同的属性名但返回不同的对象。 其中一个返回 object 和第二个列表<object><div id="text_translate"><p>我有一个这样的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想将 json 反序列化为 Pojo。 问题是我的属性名称相同 productType 但我可以期待两种不同的返回类型或数据结构。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因为属性名称相同我如何有效地使用 Jackson 注释来解决它?</li></ol><p> 我使用 rest-assured 进行反序列化,使用 Lombok 进行典型的 getter 和 setter。</p></div></object> - Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object>

暂无
暂无

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

相关问题 Java-对象列表。 在字段中查找具有特定值的对象 - Java - List of objects. Find object(s) with a certain value in a field Java,Item对象的链接列表。 如何参考当前项目? - Java, Linked List of Item objects. How to refer to the current item? Jackson (Java):反序列化相同的属性名但返回不同的对象。 其中一个返回 object 和第二个列表<object><div id="text_translate"><p>我有一个这样的 POJO:</p><pre> public class NewClass { String name; @JsonProperty("productType") ProductType productType2005; List&lt;ProductType&gt; productType; }</pre><p> 我想将 json 反序列化为 Pojo。 问题是我的属性名称相同 productType 但我可以期待两种不同的返回类型或数据结构。</p><ol><li> 返回ProductType</li><li> return List&lt;ProductType&gt;因为属性名称相同我如何有效地使用 Jackson 注释来解决它?</li></ol><p> 我使用 rest-assured 进行反序列化,使用 Lombok 进行典型的 getter 和 setter。</p></div></object> - Jackson (Java) : deserialization for the same property name but return different objects. for one it return object and second List<Object> Java链接的对象列表。 如何记录每个唯一的对象? - Java Linked List of Objects. How can I keep count of each unique object? 我想检查一列是否在实体对象列表中具有特定值。我该怎么做? - I want to check if a column has specific values in list of entity objects.?How can i do that? 减少对象列表中的整数属性 - Reducing integer attributes in a list of objects 清单 <Integer> 物体按长度顺序 - List<Integer> objects in order of length 使用lambda的复杂对象列表中的多个操作 - multiple operations in list of complex objects with lambda CDI @New on @Singleton对象。 它能做什么? - CDI @New on @Singleton objects. What it does? HashSet类对象。 什么是哈希? - HashSet of class objects. What is hashed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM