Arrays are used in java to holding more.pptx

javaashutoshtrivedi 16 views 16 slides Jul 10, 2024
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Array in java used to store multiple values can use array
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets:

String[] cars;
We have now declared a var...


Slide Content

Array in Java Part-001

Objectives Comparison of various programming techniques Introduction to Object Oriented Concepts What is an Object Abstraction, Encapsulation, Message Passing Class, Access Specifiers, Examples Advanced Object-Oriented Concepts Relationships Inheritance Abstract Classes and Polymorphism Object Oriented Design Methodology Trends in OO Technology

Arrays in Java Object Here, we have assumed that a restaurant will have only one contact number. What if the restaurant has multiple contact numbers? How can we store multiple contact numbers of a restaurant? One solution to this problem is to create more instance variables to store multiple contact numbers and initialize it with different values. After implementing, the Restaurant class will look like this.

Arrays in Java In order to implement the previous requirement, we can use various collections in Java. Here, to implement this requirement we will use one of the most common collection called  Array . An  array  is a collection of values of the same data type, stored in contiguous memory locations and referred by the same name. It holds a fixed number of values, decided at the time of array declaration. The last implementation of the Restaurant class had three different variables to store three different contact numbers. The below diagram shows how the contact numbers are stored if they are present in different variables and if they are stored in an array. 

Arrays in Java

Arrays in Java In order to use array in Java, you need to declare an array along with a datatype. You can declare, create and initialize an array in the following ways: Declaring and initializing the array in one line: Syntax :

Arrays in Java In order to use array in Java, you need to declare an array along with a datatype. You can declare, create and initialize an array in the following ways: Creating the array using new:  Syntax:

Arrays in Java Declaring and creating the array in different lines:  Syntax:

Accessing the elements of an array Each value stored in an array is called as an element. Each element in an array is accessed, stored and retrieved using its position in the array, called  index . Indexes in Java are zero-based, i.e., the valid range of indexes for the elements of an array is from 0 to (size of Array-1). long [] restaurantContacts = new long [3]; restaurantContacts [0] = 9992346725L; // Elements can be updated and accessed with the help of index restaurantContacts [1] = 9992346726L; restaurantContacts [2] = 9992346727L; System. out .println ( restaurantContacts [1]) ; // Accessing and displaying the element at the 1st index

Accessing the elements of an array using for loop Instead of writing n number of lines to access n elements of an array, you can use different looping constructs like for loop, for-each loop, etc. The below code shows how to access and display the elements of restaurantContacts with the help of for loop. public class Tester { public static void main(String[] args ) { long [] restaurantContacts = { 9992346725L, 9992346726L, 9992346727L }; for ( int index = 0; index < restaurantContacts . length ; index ++) { // Accessing element at position index System. out .println ( restaurantContacts [ index ]); } } }

Accessing the elements of an array using for each  loop Java also has another loop known as for-each loop to iterate over collections. This eliminates the use of indexes. It displays the array elements one by one. It holds an array element in a variable and then executes the body of the loop. Syntax for ( dataType variable: array) {  //body of the loop  }  public class Tester { public static void main(String[] args ) { long [] restaurantContacts = { 9992346725L, 9992346726L, 9992346727L }; for ( long contactNumber : restaurantContacts ) { System. out .println ( contactNumber ); } } }

Multi-dimensional Array Multi-dimensional arrays are arrays of arrays with each element of the array holding the reference of other arrays. A multi-dimensional array is created by appending one set of square brackets ([]) per dimension .. Let's see a simple example to declare, instantiate, initialize and display a 2-dimensional (2D) array. Syntax for creating 2D array : dataType [][]  arrayVarName = new dataType [ rowsize ][ columnsize ];

Multi-dimensional Array //Here, the row size is 7, and the column size is 2 //The 0th index stores the Max temperature and 1st index stores the Min temperature int [][] dayWiseTemperature = new int [7][2]; dayWiseTemperature [0][0]=29; // Initialization dayWiseTemperature [0][1]=21; // and so on //Another way of creating and initializing 2D array int [][] dayWiseTemperature = new int [][] { { 29,21}, { 24,23}, {26,22}, {28,23}, {29,24}, {23,20}, {29,21} };

Multi-dimensional Array

Points to remember Some of the important points that you should be knowing about arrays are: An array is a collection of similar data in contiguous memory locations referred by the same name Can be used to store data of primitive as well as reference types Holds a fixed number of values, determined at the time of array declaration Array index always starts from zero The length attribute of an array can be used to get its size Once initialized, the size of an array cannot be changed
Tags