Java Arrays
Arrays are used to store multiple values in a single variable, the array, which stores a fixed-size sequential collection of elements of the same type. A specific element in an array is accessed by its index.
One-Dimensional Arrays:
Syntax to Declare an Array in Javatype var-name[]=new type[size];
* new operator is used to allocate memory.
Array Declaration
public class MyClass { public static void main(String[] args) { String[] sub = {"HTML", "JAVA", "PHP", "ASP"}; System.out.println(sub[1]); } }
Output
Java
Loop Through an Array:
Example-Enter five array elements then print those elements.
import java.util.*; public class MyArray { public static void main(String[] argv) { Scanner obj=new Scanner(System.in); int[] n=new int[5]; for( int i=0;i<5;i++) { n[i]=obj.nextInt(); } for( int i=0;i<5;i++) { System.out.println("Array["+i+"]="+ n[i]); } } }
Output
Enter Number
5
8
3
7
2
Array[0]=5
Array[1]=8
Array[2]=3
Array[3]=7
Array[4]=2
Array Length:
We use length to find out how many elements an array has.
Example.
public class MyClass { public static void main(String[] args) { String[] sub = {"HTML", "JAVA", "PHP", "ASP"}; System.out.println(sub.length); } }
Output
4
Multidimensional Arrays
A multidimensional array is an array containing one or more arrays.
Syntax
public class MyClass { public static void main(String[] args) { int[][] n = { {1, 2, 3, 4}, {5, 6, 7} }; for (int i = 0; i < n.length; ++i) { for(int j = 0; j < n[i].length; ++j) { System.out.println(n[i][j]); } } } }
Example of a two-dimensional array
public class MyClass { public static void main(String[] args) { int[][] n = { {1, 2, 3, 4}, {5, 6, 7} }; for (int i = 0; i < n.length; ++i) { for(int j = 0; j < n[i].length; ++j) { System.out.println(n[i][j]); } } } }
Example: Enter 4 x 3 double dimension array elements then print those elements alongwith row total.
import java.util.Scanner; class Array { public static void main(String[] args) { Scanner Obj = new Scanner(System.in); int n[][] = new int[4][3]; int t []=new int[4]; int i,j; for(i=0;i<4;i++) { for(j=0;j<3;j++){ System.out.println("Enter rows and column"); n[i][j] = obj.nextInt(); } } for(i=0;i<4;i++){ for(j=0;j<3;j++){ System.out.print(" " +n[i][j]); t[i]+=n[i][j]; } System.out.print(" "+t[i]); System.out.println(); } } }
Output
4 6 7 17
3 6 7 16
5 6 4 15
9 6 5 21
Java ArrayList
Java ArrayList class uses a dynamic array for storing the elements that means resizable array, which can be found in the java.util package. It is like an array, but there is no size limit. We can add or remove elements anytime. An ArrayList automatically expands as data is added. So, it is much more flexible than the traditional array. It is like the Vector in C++.
Differences Between Array And ArrayList In Java :
- Static Vs Dynamic: Array is static, can’t change its size once it is created. Where as ArrayList is dynamic or re-sizable array. Because, it automatically resizes itself if try to add elements beyond its capacity.
- Dimensional: Array is multi-dimensional, can have one, two or three dimensional arrays. But, ArrayList is one dimensional.
- Manipulation of element: ArrayList provides methods like get(), isEmpty(), contains(), indexOf(), replaceAll()…… to manipulate its elements. Where as array doesn’t provide such methods.
- Elements Hold: Array can hold both primitive data types (int, float….) as well as objects. Where as ArrayList can hold only objects.
How to create an ArrayList?ArrayList<String> alist=new ArrayList<String>();
ArrayList<Integer> mylist=new ArrayList<Integer>();
This statement creates an ArrayList with the name alist and mylist with type “String” and "Integer". The type determines which type of elements the list will have. This list is of “String” and "Integer" type, the elements that are going to be added to this list will be of type “String” and "Integer".
How to add elements to an ArrayList?
Add elements to an ArrayList by using add() method either on specified location or at the end of ArrayList.alist.add("Java"); //Add "Java" value at the end of List.
alist.add(3, "Java"); //Add "Java" value at the fourth position of List.
Example
import java.util.*; class ListExample{ public static void main(String args[]){ ArrayList<String> alist=new ArrayList<String>(); alist.add("HTML"); alist.add("PHP"); alist.add("Python"); alist.add("ASP"); alist.add("SQL"); alist.add("JScript"); //displaying elements System.out.println(alist); //Adding "JAVA" at the fourth position alist.add(3, "JAVA"); //displaying elements System.out.println(alist); } }
Output
[HTML, PHP, Phython, ASP, SQL, JScript]
[HTML, PHP, Phython, JAVA, ASP, SQL, JScript]
How to remove elements from ArrayList?
remove() method allows to remove elements from an ArrayList from index number.
To remove all the elements in the ArrayList, use the clear() method.
Example
import java.util.*; class ListExample{ public static void main(String args[]){ ArrayList<String> alist=new ArrayList<String>(); alist.add("HTML"); alist.add("PHP"); alist.add("Python"); alist.add("ASP"); alist.add("SQL"); alist.add("JScript"); //displaying elements System.out.println(alist); //Removing "HTML" and "ASP" alist.remove("HTML"); alist.remove("ASP"); //displaying elements System.out.println(alist); //Removing 4th element alist.remove(3); //displaying elements System.out.println(alist); } }
Output
[HTML, PHP, Phython, ASP, SQL, JScript]
[PHP, Phython, JAVA, SQL, JScript]
[PHP, Phython, JAVA, JScript]
Loop Through an ArrayList
Use ArrayList with a for loop, and use the size() method to specify how many times the loop should run.
Example to add, display and remove element from ArrayList.
import java.util.ArrayList; import java.util.Scanner; public class arraylist { public static void main(String [] args) { Scanner obj=new Scanner(System.in); ArrayList<Integer> n=new ArrayList<Integer(); int no; for(int i=0;i<5;i++) { System.out.println("Enter No."); no=obj.nextInt(); n.add(no); } System.out.println("Array List="+n.size()); System.out.print(n); System.out.println(); System.out.println("Enter Position:"); int p=obj.nextInt(); System.out.println("Enter No. to add:"); no=obj.nextInt(); n.add(p,no); System.out.println("Now list is:"); System.out.print(n); System.out.println(); System.out.println("Enter position to remove:"); p=obj.nextInt(); n.remove(p); System.out.println("Now list is:"); System.out.print(n); } }
Output
Change an element in ArrayList
Use the set method to change an element in ArrayList. This method updates the element present at the given index with the new given element.
Example
import java.util.ArrayList; public class JavaExample { public static void main(String[] args) { ArrayList alist = new ArrayList(); alist.add("JAngular"); alist.add("JScript"); alist.add("JQuery"); alist.add("Java"); alist.set(0, "JSON"); System.out.println(alist); } }
Output
0 Comments