List<Object> objects = new ArrayList<>(); ArrayList<Object> objects1 = new ArrayList<>();这俩种写法有什么区别
时间: 2024-03-29 10:41:30 浏览: 97
这两种写法的区别在于对象的类型。
第一种写法中,`List<Object> objects = new ArrayList<>();`创建了一个名为`objects`的`List`对象,该列表可以存储任何类型的对象。也就是说,可以向该列表中添加任何类型的对象,包括字符串、整数、自定义对象等。
而第二种写法中,`ArrayList<Object> objects1 = new ArrayList<>();`创建了一个名为`objects1`的`ArrayList`对象,该列表只能存储`Object`类型的对象。也就是说,只能向该列表中添加`Object`类型的对象,不能存储其他类型的对象。
需要注意的是,第二种写法中的`ArrayList<Object>`并不是一个泛型类,而是一个声明了Object类型的ArrayList类。而第一种写法中的`List<Object>`则是一个泛型接口,可以用于声明各种类型的列表,如`List<String>`、`List<Integer>`等。
因此,如果你想要创建一个可以存储任何类型对象的列表,可以使用第一种写法;如果你只想存储Object类型的对象,可以使用第二种写法。
相关问题
List<RowStyle> rowStyles = new ArrayList<>();
This line of code creates a new ArrayList object called "rowStyles" that can hold objects of type "RowStyle". The "<>" notation indicates that this is a generic ArrayList, meaning it can hold any type of object as long as it is a subclass of "RowStyle". The "List" interface is implemented by ArrayList, which means that ArrayList can be used in any context where a List is expected.
List<Map<String, Object>> maps = new ArrayList<>();
This code declares an empty ArrayList of maps. Each map in the list will have keys that are strings and values that are objects. The type of objects that can be stored in the map is not specified here. The list can be used to store a collection of maps, each representing a set of key-value pairs.
阅读全文