Java数组 vs ArrayList vs List

huangapple go评论69阅读模式
英文:

Java array vs ArrayList vs List

问题

我试图理解数组(array)、ArrayList 和 List 之间的关系。

假设我想通过将一个 数组 转换为 ArrayList,然后再转换为 HashSet,最后再转换回 数组 来去除重复项。

int[] start = {1, 2, 3, 4, 5};
....
....
....
int[] result = ....

这将如何工作?

英文:

Im trying to understand the relationship between array, ArrayList, and List.

Say I wanted to remove duplicates by converting an array into an ArrayList then into a HashSet, then returned to an array.

int[] start = {1,2,3,4,5};
....
....
....
int[] result = .....

How would that work?

答案1

得分: 1

int[] array = {1, 2, 3, 4, 5};
for(int i : array) {
    System.out.print(i + ", ");
}
System.out.println();
ArrayList myArrayList = new ArrayList();
for(int num : array) {
    myArrayList.add(num);
}
System.out.println(myArrayList);
HashSet<Integer> myHashSet = new HashSet<Integer>();
for(int i = 0; i < myArrayList.size(); i++) {
    myHashSet.add((Integer) myArrayList.get(i));
}
System.out.println(myHashSet);
This is an example of a normal int array converted to an ArrayList converted to a HashSet.
英文:
    int[] array = {1, 2, 3, 4, 5};
	for(int i : array) {
		System.out.print(i+&quot;, &quot;);
	}
	System.out.println();
    ArrayList myArrayList = new ArrayList();
    for(int num : array) {
    	myArrayList.add(num);
    }
    System.out.println(myArrayList);
    HashSet&lt;Integer&gt; myHashSet = new HashSet&lt;Integer&gt;();
    for(int i = 0;i&lt;myArrayList.size();i++) {
    	myHashSet.add((Integer) myArrayList.get(i));
    	
    }
    System.out.println(myHashSet);

This is an example of a normal int array converted to an ArrayList converted to a HashSet.

答案2

得分: 1

Array是一个固定长度的数据结构。它是一块连续的内存区域。假设你在x3000处有一个长度为2的数组。数组会位于x3000和x3001处。请记住,这是对概念的过度简化,因为元素大小和每个内存位置的大小肯定会影响数组元素的位置(并不总是以x3001结尾)。

List是一个抽象数据类型(ADT),listarray之间显著的区别是list没有固定的长度。可能还有其他差异,但可能因情况而异。

ArrayList是一个使用数组实现的ADT列表。就像在使用数组构建栈、队列等需要解决问题时一样。

英文:

Array is a fixed length data structure. It is a continuous block of memory. Let say you have an array at x3000 with a length of 2. An array stay at x3000 and x3001. Remember, this is oversimplifying the concept, as element size and size per memory location will certainly affect the position of array's elements (not always end at x3001).

List is an abstract data type (ADT), the notable difference between list and array is list has no fixed length. There can be more differences but it may vary from case to case.

ArrayList is a ADT list, but implemented using array. It's like when you do problems that require constructing stack, queue, etc using array.

答案3

得分: -1

数组,ArrayList和List之间的关系。

数组是使用您在问题中展示的[]{}符号创建的。它不是动态分配的。

List是一个接口,它只是其实现必须响应的方法的指南。

ArrayList是List的一种实现。

英文:

> the relationship between array, ArrayList, and List.

An array is created with the [] and {} notation you showed in your question. It's not dynamically allocated.

A List is an interface, which is just a guide to what methods its implementations must respond to.

An ArrayList is an implementation of a List.

huangapple
  • 本文由 发表于 2020年10月6日 11:56:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64219138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定