Arrays in java || how to store more than 1 value in one variable

Arrays is a data structure that is used to store multiple value in single variable .


How to initilaiz array

Syntex:-

DataType[] arrayName = new Datatype[n

where n is number of value in arrays

 

 Ex. 1:-

package ……;
public class …. {

public static void main(String[] args) {

int [] marks = new int[5];

marks[0] = 100;
marks[1] = 90;
marks[2] = 50;
marks[3] = 70;
marks[4] = 34;

}
}

Here marks[0], marks[1] , marks[2] marks[3] …. is used to store the numbers where marks[0] is representing 1st number or 1st place

Also we can direct intializ the number:-

ex.-  int [] age = {12,3,21,43,53,3,2,7};

How to print the numbers –

ex.  if you want to print 1st value which is equal to 100 then

package ……..;
public class …. {

public static void main(String[] args) {

int [] marks = new int[5];

marks[0] = 100;
marks[1] = 90;
marks[2] = 50;
marks[3] = 70;
marks[4] = 34;

System.out.println(marks[0]);

}
}

 

How to change the number after intializing :-

let we have given a array int [] marks = {22,44,5 ,56,43,89,55}; and we have to change 3rd number’s value which is equal to 5 

code:

package …….;
public class …… {

public static void main(String[] args) {

int [] marks = {22,44,3,56,43,89,55};

marks[2] = 10;

System.out.println(marks[2]);
}
}

 

Rishabh

I want to change the world with the help of tech 🙃

Leave a Reply