Day 12
Day 12
Java array
• 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:
int num = 69;
int player1 = 56;
int player2 =60;
Int player3=90;
int[] players={56,67,89,23,45,68,2,34,78,89,100}
Int[] roollnos={1,2,3,4,5,6}
String[] StudensName={“Sidhu”, “Krishna”,
“Aniket”}
Array
String[] cars; int[] nums={1,2,4,6,8,9};
We have now declared a variable that holds an
array of strings. To insert values to it, you can
place the values in a comma-separated list,
inside curly braces:
•
String[] cars = {"Volvo", "BMW", "Ford",
"Mazda"};
Array
To create an array of integers, you could write:
int[] myNum = {10, 20, 30, 40};
Access the Elements of an Array
0 1 2 3
Volvo BMW Ford Mazda
int[] nums={1,2,3,4,5};
Array
• Write a program to declare 5 elements inter
array
Program to print all elements of array