-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaarrays.java
59 lines (47 loc) · 1.33 KB
/
Javaarrays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package basics;
public class Javaarrays {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] cars= {"Volvo","BMW","TATA","Audi"};
int[] myNum= {10,20,30,40,50};//Array of integers
//accessing elements of an array
System.out.println(cars[0]);
System.out.println(myNum[myNum.length-1]);
//changing array element
cars[0]="Maruti";
System.out.println(cars[0]);
//length of array
System.out.println("array length of myNum: "+myNum.length);
//loops through an array
for(int i=0;i<cars.length;i++)
{
System.out.println(cars[i]);
}
System.out.println("___________________");
//loop through an array with for each
for(int i:myNum)
{
System.out.println(i);
}
System.out.println("___________________");
//declaring array without any values
int[] myNumbers = new int[5];
for(int i=0;i<myNumbers.length;i++)
{
System.out.println(myNumbers[i]);
}
System.out.println("___________________");
//adding value to an array
int[] myNumbers1 = new int[5];
myNumbers1[0]=100;
myNumbers1[1]=200;
myNumbers1[2]=300;
myNumbers1[3]=400;
myNumbers1[4]=500;
for(int i=0;i<myNumbers1.length;i++)
{
System.out.println(myNumbers1[i]);
}
System.out.println("___________________");
}
}