SessionPlans_f3efa1.6 Array in java.pptx the java topic array of data structuere and introduction of java, and it will help you in the future , i am 100% sure about it
Size: 200.09 KB
Language: en
Added: Sep 18, 2024
Slides: 26 pages
Slide Content
Array
An array is a collection of similar types of data. For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. String [] array = new String[100 ]; Here, the above array cannot store more than 100 names. The number of values in a Java array is always fixed.
Advantages Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently. Random access: We can get any data located at an index position . Disadvantages Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.
How to declare an array in Java? Syntax : dataType [] arrayName ; dataType - it can be primitive data types like int , char, double, byte, etc. or Java objects arrayName - it is an identifier For example, double [] data ; But , how many elements can array this hold?
To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example, // declare an array double [] data; // allocate memory data = new double[10];
In Java, we can declare and allocate the memory of an array in one single statement. For example, double [] data = new double[10];
Initialize Arrays in Java we can initialize arrays during declaration. For example, //declare and initialize and array int [] age = {12, 4, 5, 2, 5};
In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number . For example, // declare an array int [] age = new int [5]; // initialize array age[0 ] = 12; age[1 ] = 4; age[2 ] = 5; ..
Access Elements of an Array in Java We can access the element of an array using the index number . // access array elements array[index ]
class Main { public static void main(String[] args ) { // create an array int [] age = {12, 4, 5, 2, 5}; // access each array elements System.out.println ("Accessing Elements of Array:"); System.out.println ("First Element: " + age[0]); System.out.println ("Second Element: " + age[1]); System.out.println ("Third Element: " + age[2]); System.out.println ("Fourth Element: " + age[3]); System.out.println ("Fifth Element: " + age[4]); } }
Looping Through Array Elements class Main { public static void main(String[] args ) { // create an array int [] age = {12, 4, 5 }; // loop through the array using for loop System.out.println ("Using for Loop:"); for( int i = 0; i < age.length ; i ++) { System.out.println (age[ i ]); } } }
Using the for-each Loop class Main { public static void main(String[] args ) { // create an array int [] age = {12, 4, 5}; // loop through the array using for loop System.out.println ("Using for-each Loop:"); for( int a : age) { System.out.println (a); } } }
Taking array through user Java does not provide any direct way to take array input . But we can take array input by using the method of the Scanner class. To take input of an array, we must ask the user about the length of the array.
One-dimensional array input import java.util.Scanner ; public class ArrayInputExample1 { public static void main(String[] args ) { int n; Scanner sc =new Scanner(System.in); System.out.print ("Enter the number of elements you want to store: "); // reading the number of elements from the that we want to enter n= sc.nextInt (); // creates an array in the memory of length 10 int [] array = new int [10]; System.out.println ("Enter the elements of the array: ");
for( int i =0; i <n; i ++) { // reading array elements from the user array[ i ]= sc.nextInt (); } System.out.println ("Array elements are: "); // accessing array elements using the for loop for ( int i =0; i <n; i ++) { System.out.println (array[ i ]); } } }
Java Multidimensional Arrays A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself . For example, int [][] a = new int [3][4];
How to initialize a 2d array in Java? Here is how we can initialize a 2-dimensional array in Java. int [][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, };
Example: 2-dimensional Array class MultidimensionalArray { public static void main(String[] args ) { // create a 2d array int [][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, }; // calculate the length of each row System.out.println ("Length of row 1: " + a[0].length); System.out.println ("Length of row 2: " + a[1].length); System.out.println ("Length of row 3: " + a[2].length); } }
Printing all elements of 2d array Using Loop class MultidimensionalArray { public static void main(String[] args ) { int [][] a = { {1, -2, 3}, {-4, -5, 6, 9}, {7}, }; for ( int i = 0; i < a.length ; ++ i ) { for( int j = 0; j < a[ i ].length; ++j) { System.out.println (a[ i ][j]); } } } }
for...each loop to access elements class MultidimensionalArray { public static void main(String[] args ) { // create a 2d array int [][] a = { {1, -2, 3}, {-4, -5, 6, 9}, {7}, }; // first for...each loop access the individual array inside the 2d array for ( int [] innerArray : a) { // second for...each loop access each element inside the row for( int data: innerArray ) { System.out.println (data); } } } }
Two-dimensional array input A two-dimensional array is an array that contains elements in the form of rows and columns. It means we need both row and column to populate a two-dimensional array. Matrix is the best example of a 2D array. We can declare a two-dimensional array by using the following statement. datatype arrayName [][] = new datatype [m][n];
import java.util.Scanner ; public class ArrayInputExample2 { public static void main(String args []) { int m, n, i , j; Scanner sc =new Scanner(System.in); System.out.print ("Enter the number of rows: "); //taking row as input m = sc.nextInt (); System.out.print ("Enter the number of columns: "); //taking column as input n = sc.nextInt (); // Declaring the two-dimensional matrix int array[][] = new int [m][n]; // Read the matrix values System.out.println ("Enter the elements of the array: ");
//loop for row for ( i = 0; i < m; i ++) //inner for loop for column for (j = 0; j < n; j++) array[ i ][j] = sc.nextInt (); //accessing array elements System.out.println ("Elements of the array are: "); for ( i = 0; i < m; i ++) { for (j = 0; j < n; j++) // prints the array elements System.out.print (array[ i ][j] + " "); // throws the cursor to the next line System.out.println (); } } }
String Array import java.util.Scanner ; public class StringArrayInput { public static void main(String[] args ) { Scanner sc = new Scanner(System.in); System.out.println ("Enter the size of the array"); int n = sc.nextInt (); String[] arr = new String[n]; System.out.println ("Enter the elements of the array"); for ( int i = 0; i < n; i ++) { arr [ i ] = sc.next (); } System.out.println ("The elements of the array are"); for ( int i = 0; i < n; i ++) { System.out.println ( arr [ i ]); } } }
An Array Of Characters From User Input import java.util.Scanner ; public class TakeCharArrayInputfor { public static void main(String args []){ //scanner class to read input from the user Scanner sc =new Scanner(System.in); //Declaring and creating character array char[] arr =new char[5]; //initializing value to the array System.out.println ("******Initializing array******"); System.out.println ("Enter character values"); arr = sc.next (). toCharArray (); //Take character input //displaying the array elements System.out.println ("\n******displaying array of characters******"); System.out.println ("Entered Characters are"); for( int i =0; i < arr.length ; i ++){ System.out.print ( arr [ i ]+"\t"); } } }