ARRAYLIST
Hey guys, we are
back with another blog, in this we are talking about arraylists in java. So
happy reading and do let us know your views on this in the comment section
below.
Arraylist is another
kind of array in java, but in this you can do certain operations and
customizations, also the size of an arraylist is not fixed. So before
implementation, we need to import the array list class i.e., import
java.util.ArrayList;
After importing the arraylist class we have to create an
object, let’s see the syntax
ArrayList <Type> name = new
ArrayList();
Now after creating an object we can add the values of the
type that we have already wrapped within the angular brackets when we made the
arraylist object, to add values we use the add() function and we put the
values to be added inside the parentheses, separately for each value.
Ex:
package arrayList;
import java.util.ArrayList;
public class myArrayList {
public static void main(String[] args) {
ArrayList
<Integer> marks = new
ArrayList();
marks.add(99);
marks.add(85);
marks.add(71);
System.out.println(marks);
}
}//Output:
[99, 85, 71]
Also the point to be noted here is that we use the proper
word to declare the type i.e., ‘Integer’ instead of ‘int’, same holds with the
char type we will use ‘Character’ (inside the angular brackets) if we want our
element to be of type char. Also (as shown above) in order to view the elements
of an arraylist one doesn’t needs to use a loop, they can just print the name
of the arraylist object.
Now let’s see another example in which we add char type in
our arraylist.
import java.util.ArrayList;
public class myArrayList {
public static void main(String[] args) {
ArrayList
<Character> xyz = new
ArrayList();
xyz.add('C');
xyz.add('o');
xyz.add('d');
xyz.add('i');
xyz.add('n');
xyz.add('g');
xyz.add('W');
xyz.add('i');
xyz.add('n');
xyz.add('d');
xyz.add('s');
System.out.println(xyz);
}
}//Output: [C, o, d, i, n,
g, W, i, n, d, s]
Similarly if we add Strings in our arraylist we will wrap it
within double quotes inside the parentheses after add.
Now let’s us see how can we take user inputs in our
arraylist, below is the example which demonstrates how can we take user inputs
for a string type
import java.util.*;
public class myArrayList {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
ArrayList
<String> fruits = new ArrayList();
fruits.add(sc.nextLine());
fruits.add(sc.nextLine());
fruits.add(sc.nextLine());
System.out.println(fruits);
}
}//Output:
[banana, orange, pineapple]
So now let’s talk about some of the functions that can be
used for arraylist :
i) .get(index) :
So basically the get keyword
gets us the value of the element at the index (that will be inside the
parentheses following get.
Ex:
import java.util.*;
public class myArrayList {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
ArrayList
<String> fruits = new ArrayList();
fruits.add("banana");
fruits.add("orange");
fruits.add("pineapple");
System.out.println(fruits.get(2));
}
}//Output: pineapple
ii)add():
We have already seen the use of the add()
function but we can also use this function to add an element at specific index.
Ex:
import java.util.*;
public class myArrayList {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
ArrayList
<String> text = new
ArrayList();
text.add("Winds");
text.add(0, "Coding");
System.out.println(text);
}
iii).set(index, value):
Set functions modifies
(or replaces) the value of the element at the specified index.
Ex:
import java.util.*;
public class myArrayList {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
ArrayList
<String> text = new ArrayList();
text.add("Coding");
text.add("Winds");
text.set(0,"Hello");
text.set(1, "People");
System.out.println(text);
}
iv).remove(index):
This
function removes the element at the specified index.
Ex:
import java.util.*;
public class myArrayList {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
ArrayList
<String> text = new ArrayList();
text.add("Coding");
text.add("Winds");
text.remove(1);
System.out.println(text);
}
}//Output:[Coding]
v).remove():
This
function is used to clear or delete all the elements from the arraylist.
Ex:
import java.util.*;
public class myArrayList {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
ArrayList
<String> text = new ArrayList();
text.add("Coding");
text.add("Winds");
text.clear();
System.out.println(text);
}
vi).size():
This tells us the size of the arraylist in java.
Hope you are clear on this topic do read our more articles on JAVA LANGUAGE.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
This article is SUBMITTED By : Pranjal Rai
Do subscribe to our daily blog update by clicking here.
Thank You!
No comments:
Post a Comment