Contents
Kinh Nghiệm Hướng dẫn Should I use ArrayList or array? 2022
Quý khách đang tìm kiếm từ khóa Should I use ArrayList or array? được Cập Nhật vào lúc : 2022-12-16 12:04:17 . Với phương châm chia sẻ Mẹo về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi tìm hiểu thêm tài liệu vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.
Array and ArrayList are most used data types while developing any java applications. Both are used to store group of objects. In this post I have tried to list down the advantages of using ArrayList over Arrays.Before discussing the advantages of ArrayList, lets see what are the drawbacks of arrays.
- Arrays are of fixed length. You can not change the size of the arrays once they are created.You can not accommodate an extra element in an array after they are created.Memory is allocated to an array duringits creation only,muchbefore the actual elements are added to it.
Because of these drawbacks, use of arrays are less preferred. Instead of arrays, you can use ArrayList class which addresses all these drawbacks. Here are some advantages of using ArrayList over arrays.
1) You can define ArrayList as re-sizable array. Size of the ArrayList is not fixed. ArrayList can grow and shrink dynamically.
class ArrayListDemo
public static void main(String[] args)
ArrayList list = new ArrayList();
list.add(“ONE”);
list.add(“TWO”);
list.add(“THREE”);
System.out.println(list.size()); //Output : 3
//Inserting some more elements
list.add(“FOUR”);
list.add(“FIVE”);
System.out.println(list.size()); //Output : 5
//Removing an element
list.remove(“TWO”);
System.out.println(list.size()); //Output : 4
2) Elements can be inserted or deleted from a particular position.
class ArrayListDemo
public static void main(String[] args)
ArrayList list = new ArrayList();
list.add(“ZERO”);
list.add(“TWO”);
list.add(“FOUR”);
System.out.println(list); //Output : [ZERO, TWO, FOUR]
list.add(2, “THREE”); //Inserting an element index 2
list.add(1, “ONE”); //Inserting an element index 1
System.out.println(list); //Output : [ZERO, ONE, TWO, THREE, FOUR]
list.remove(3); //Removing an element from index 3
System.out.println(list); //Output : [ZERO, ONE, TWO, FOUR]
3) ArrayList class has many methods to manipulate the stored objects.
ArrayList class has methods to perform solomodifications ( add(), remove() ), bulk modifications ( addAll(), removeAll(), retainAll() ), searching( indexOf(), lasIndexOf() ) and iterations( iterator() ).
4) If generics are not used, ArrayList can hold any type of objects.
class ArrayListDemo
public static void main(String[] args)
ArrayList list = new ArrayList(); //ArrayList without generics
list.add(“ZERO”); //adding string type object
list.add(1); //adding primitive int type
list.add(20.24); //adding primitive double type
list.add(new Float(23.56)); //Adding Float wrapper type object
list.add(new Long(25)); //Adding Long wrapper type object
System.out.println(list); //Output : [ZERO, 1, 20.24, 23.56, 25]
5) Many are of the assumption that multiple insertion and removal operations on ArrayList will decrease the performance of an application. But, there will be no significant change in the performance of an application if you use ArrayList instead of arrays. Below example shows time taken to add 1000 string elements to ArrayList and array.
class ArrayListDemo
public static void main(String[] args)
String[] namesArray = new String[1000];
long startTime = System.currentTimeMillis();
for (int i = 0; i < namesArray.length; i++)
namesArray[i] = "Name"+i;
long endTime = System.currentTimeMillis();
System.out.println("Time taken by Array : "+(endTime – startTime)+"ms");
ArrayList nameList = new ArrayList();
startTime = System.currentTimeMillis();
for (int i = 0; i <= 1000; i++)
nameList.add("Name"+i);
endTime = System.currentTimeMillis();
System.out.println("Time taken by ArrayList : "+(endTime-startTime)+"ms");
Output :
Time taken by Array : 6ms
Time taken by ArrayList : 6ms
6) You can traverse anArrayList in both the directions forward and backward using ListIterator.
class ArrayListDemo
public static void main(String[] args)
ArrayList list = new ArrayList();
list.add(“ONE”);
list.add(“TWO”);
list.add(“THREE”);
list.add(“FOUR”);
ListIterator iterator = list.listIterator();
System.out.println(“Elements in forward direction”);
while (iterator.hasNext())
System.out.println(iterator.next());
System.out.println(“Elements in backward direction”);
while (iterator.hasPrevious())
System.out.println(iterator.previous());
7) ArrayList can hold multiple null elements.
class ArrayListDemo
public static void main(String[] args)
ArrayList list = new ArrayList();
list.add(100);
list.add(null);
list.add(null);
System.out.println(list); //Output : [100, null, null]
8) ArrayList can hold duplicate elements.
class ArrayListDemo
public static void main(String[] args)
ArrayList list = new ArrayList();
list.add(100);
list.add(100);
list.add(100);
System.out.println(list); //Output : [100, 100, 100]
(Above two advantages(7 and 8) are also applicable to arrays. But, you can treat them as bonus with all above advantages of ArrayList.)
Clip Should I use ArrayList or array? ?
Bạn vừa tìm hiểu thêm tài liệu Với Một số hướng dẫn một cách rõ ràng hơn về Clip Should I use ArrayList or array? tiên tiến và phát triển nhất
Bạn đang tìm một số trong những Chia SẻLink Tải Should I use ArrayList or array? miễn phí.
Giải đáp vướng mắc về Should I use ArrayList or array?
Nếu sau khi đọc nội dung bài viết Should I use ArrayList or array? vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha
#ArrayList #array