Array
Array
Part-001
◌ 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.
◌ 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.
◌ Syntax:
◌ Syntax:
◌ Syntax:
◌ The below code shows how to access and display the elements of restaurantContacts with the help of for loop.
Syntax
for (dataType variable: array) {
//body of the loop
}
◌ 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.
//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}
};
Irvine Mumbai
41 Corporate Park Unit No. 74, 2nd Floor, SDF 3
Suite 390 SEEPZ, Andheri East
Irvine, CA 92606 Mumbai – 400 096, India
Phone: 949-223-5100 Phone: +91 22 6114 6969
Austin Bengaluru
111 W. Anderson Lane #7, 18th Main Road, 7th Block
Suite E336 Koramangala
Austin, TX 78752 Bengaluru - 560 095, India
Phone: 512-212-4070 Phone: +91 80 4110 4560
Phoenix Dubai
21410 North 19 Ave
th Office No. 255, Building No. 17
Suite 114 Dubai Internet City
Phoenix, AZ 85027 Dubai, UAE
Contact : Phone: 602-455-1860 Phone: +971-4-458-7336
London Singapore
Millbank Tower, Citibase 21-24 144 Robinson Road, #13-01
Millbank, office no 1.7 Robinson Square
London SW1P 4DP Singapore 068908
Phone: +44 (0)20 3355 7594 Phone: +65 62741455