java ArrayList 对象存储在 List 变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14492258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayList object stored in List variable
提问by liv2hak
I am newly learning java.I am trying to understand the various data structures in util package.
我是新学java.我想了解util包中的各种数据结构。
/*create an ArrayList object*/
List<String> arrayList = new ArrayList<String>();
In the decleration of ArrayList class I have seen
在 ArrayList 类的声明中,我看到了
public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable
My question is if I use an object of type List<> to store the object returned by constructor of ArrayList<>
我的问题是我是否使用 List<> 类型的对象来存储由 ArrayList<> 的构造函数返回的对象
will it work.You may assume that I need to use only the methods specified in the List interface,such as add() or size().
它会起作用吗?您可能假设我只需要使用 List 接口中指定的方法,例如 add() 或 size()。
I have tried this on my machine and it does not work.I found this in the java tutorial below
我在我的机器上试过这个,但它不起作用。我在下面的 java 教程中找到了这个
The complete example code that I tried is as below.
我试过的完整示例代码如下。
package arraylist;
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args)
{
/*create an ArrayList object*/
List<String> arrayList = new ArrayList<String>();
/*
Add elements to Arraylist using
boolean add(Object o) method. It returns true as a general behavior
of Collection.add method. The specified object is appended at the end
of the ArrayList.
*/
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
/*
Use get method of Java ArrayList class to display elements of ArrayList.
Object get(int index) returns and element at the specified index in
the ArrayList
*/
System.out.println("Getting elements of ArrayList");
System.out.println(arrayList.get(0));
System.out.println(arrayList.get(1));
System.out.println(arrayList.get(2));
}
}
I forgot to import List.that is why it did not work.However my next question is we use a variable of type List to store on object of type ArrayList.
我忘记导入 List。这就是它不起作用的原因。但是我的下一个问题是我们使用 List 类型的变量来存储在 ArrayList 类型的对象上。
Comparing the methods of List and ArrayList ArrayList has a method called trimToSize() that is not there in list.
比较List 和ArrayList 的方法 ArrayList 有一个叫做trimToSize() 的方法,它在list 中是没有的。
In the above case will I be able to call that method?
在上述情况下,我可以调用该方法吗?
If yes,what that generally means is that a base class pointer can be used to store a derived class object as the method list of derived class will always be a super-set of base class methods?
如果是,那通常意味着基类指针可用于存储派生类对象,因为派生类的方法列表将始终是基类方法的超集?
Is my above conclusion correct? It my sound stupid question but I am very new to java.
我上面的结论对吗?这听起来很愚蠢的问题,但我对 Java 很陌生。
回答by Kevin Bowersox
Since ArrayList
implements the List
interface you should be able to use the object declared as a List
that is initialized to an ArrayList
. This is pretty common practice. It allows you to switch the implementation of the list without code changes.
由于ArrayList
实现了List
接口,您应该能够使用声明为List
初始化为 a 的对象ArrayList
。这是很常见的做法。它允许您在不更改代码的情况下切换列表的实现。
Make sure your importing the list interface:
确保您导入列表界面:
package arraylist;
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args)
{
/*create an ArrayList object*/
List<String> arrayList = new ArrayList<String>();
/*
Add elements to Arraylist using
boolean add(Object o) method. It returns true as a general behavior
of Collection.add method. The specified object is appended at the end
of the ArrayList.
*/
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
/*
Use get method of Java ArrayList class to display elements of ArrayList.
Object get(int index) returns and element at the specified index in
the ArrayList
*/
System.out.println("Getting elements of ArrayList");
System.out.println(arrayList.get(0));
System.out.println(arrayList.get(1));
System.out.println(arrayList.get(2));
}
}
When referencing the List
you will not be able to use the trimToSize
function since it is not on the interface for List
引用时List
您将无法使用该trimToSize
功能,因为它不在界面上List
回答by Jayamohan
Consider this example,
考虑这个例子,
class A implements B { }
In this case A class may be assigned to a variable of the type of one of its implemented interfaces.
在这种情况下,可以将类分配给其实现的接口之一类型的变量。
C c = new A();
If you do this casting when you use instance of c, only the methods available in Class C will be accessible.
如果在使用 c 的实例时执行此转换,则只能访问 C 类中可用的方法。
Similarly ArrayList
is your ClassA and your List
is Class B in example.
同样ArrayList
是你的 ClassA 和你List
的 Class B 在示例中。
In the below using arraylist
instance you can invoke methods available in List interface,
在下面的使用arraylist
实例中,您可以调用 List 接口中可用的方法,
/*create an ArrayList object*/
List<String> arrayList = new ArrayList<String>();
And If you use the above assignment you cannot call ArrayList methods like trimToSize()
etc. If you want to use ArrayList methods just use as below.
如果你使用上面的赋值,你不能调用 ArrayList 方法等trimToSize()
。如果你想使用 ArrayList 方法,请使用如下。
/*create an ArrayList object*/
ArrayList<String> arrayList = new ArrayList<String>();
Most of the developers, 90% of the time use the methods provided by List and thats the reason all the example out there is following the former method.
大多数开发人员,90% 的时间使用 List 提供的方法,这就是所有示例都遵循前一种方法的原因。
回答by Recurse
Edit the top of the file to look like this:
将文件顶部编辑为如下所示:
package arraylist; import java.util.ArrayList; import java.util.List;
包数组列表;导入 java.util.ArrayList; 导入 java.util.List;
....
....
If you haven't imported List, you can't use it as the type for a variable. This is the problem you are experiencing.
如果您尚未导入 List,则不能将其用作变量的类型。这是您遇到的问题。
回答by Patashu
List<String>
arrayList means 'this variable can only store implements of the List<String>
interface'
List<String>
arrayList 表示“这个变量只能存储List<String>
接口的实现”
ArrayList<String>
implements List<String>
so it can live there.
ArrayList<String>
工具,List<String>
所以它可以住在那里。