ArrayList in Java || Java and DSA course || Blog 10

We know what is an Array. An array is a collection of similar data elements stored at contiguous memory locations. It has a fixed size.

If we want to add value to our array whose current size is 7 and elements present are 7, its not possible. Even if we don't have continuous memory, we cannot make an array.

These were some drawbacks of Array. ArrayList solves this drawbacks.ArrayList connects to each memory. It is non-continuous. the size of ArrayList is a variable. it change according to us. It can store Primitive Data type and Objects. To know more about Primitive Data types, visit:- 

https://unnatikadamjavadsa.blogspot.com/2022/06/data-types-in-java-java-dsa-course-blog.html

ArrayList is a part of collection framework 


 

For a quick overview we will be learning about :

  • add 
  • get
  • insert
  • set
  • delete
  • size
  • loops/iterate
  • sorting

We need to import package named as java.util.ArrayList

To make an ArrayList we use,

ArrayList<Integer> list1 = new ArrayList<Integer>(); 

list1 is the name, integer the type of array new is the keyword to make ArrayList.

 Add

import java.util.ArrayList;
public class blog{
public static void main (String[] args) {
/* code */
ArrayList <Integer> list1 = new ArrayList<Integer>();
//System.out.println(lst);
list1.add(11); ///add 11 in list1
list1.add(12); ///add 12 in list1
list1.add(13); ///add 13 in list1
System.out.println(list1);
}
}

The output will be 

[11, 12, 13]

Here we use .add function to add integer . their index depends when they are added.

Get

We try to get one of the integer in array using index of the value. lets say we want the number present on index 1(which is 12)

System.out.println(list1.get(1));

The output will be 12. we used the .get() here.

Insert

Lets say we want to insert our 22 at index 1 where 12 is there.

list1.add(1,22);
list1.add(1,22);
System.out.println(list1);

The output will be 

[11, 22, 12, 13]

We passed our index value and our number in .add()

Set

Now we dont to insert our element, instead we want to change 22 with 32

list1.set(1,32);
System.out.println(list1);

 The output will be

[11, 32, 12, 13]

Delete

Lets remove 32 from list

list1.remove(3);
System.out.println(list1);

The output will be:

[11, 22, 12]

Here we use .remove() and pass the index which we want to remove.

Size

Right now , how much elements do we have in total. we can use .size() to find out.

System.out.println(list1.size());

It will print 3

Loops

We are going to use loop to print our array

for(int i =0;i<list1.size();i++){
System.out.println(list1.get(i));
}

The output is

11
22
12

Sorting

in ArrayList we have a .sort() function which sorts our list.

We need to import java.util.Collections

Collections.sort(list1);
System.out.println(list1);

The output is:

[11, 12, 22]

This was all about ArrayList which we will be using in Data Strucures and algorithms.

 __________________________________________________________________________________

Checkout:https://javaanddsa.blogspot.com/

Did you like our blog 10??

Tell us in the comment section

 

Comments

Post a Comment

Popular posts from this blog

LinkedList Problems for Beginners DSA || Java and DSA course || Blog 17

Introduction to Java || Java + DSA course || Blog 1

LinkedList in DSA || Java and DSA course || Blog 16