-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayConcept.java
More file actions
140 lines (105 loc) · 3.09 KB
/
Copy pathArrayConcept.java
File metadata and controls
140 lines (105 loc) · 3.09 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package JavaSessions;
public class ArrayConcept {
public static void main(String[] args) {
//array:
//1. static array: size is fixed
//limitations of static array:
//a. size is fixed(static): extra memory and less memory
//b. can not store diff types of data
//to overcome for both the limitations: we have to use dynamic array: Collections-> ArrayList
//2. dynamic array: dynamic sized
//int array:
int i[] = new int[4];
i[0] = 10;
i[1] = 20;
i[2] = 30;
i[3] = 40;
//i[4] = 50;//ArrayIndexOutOfBoundsException
//i[-1] = 60;//ArrayIndexOutOfBoundsException
System.out.println(i);
System.out.println("Li = " + 0);
System.out.println("length =" + i.length);
int l = i.length;//4
System.out.println("Hi = " + (l-1));//3
System.out.println(i[0]);
System.out.println(i[3]);
//System.out.println(i[4]);//ArrayIndexOutOfBoundsException - RunTime
//System.out.println(i[-1]);//ArrayIndexOutOfBoundsException - RunTime
System.out.println("------typical for loop---------");
//to print all the values from array: for loop:
for(int k=0; k<i.length; k++) { //O(n) - linear
System.out.println(i[k]);//10 20 30 40
}
System.out.println("---------------");
//for each loop:
for(int e : i) { //O(n) -- linear
System.out.println(e);
}
System.out.println("---------------");
System.out.println(i[0] + i[3]);
//System.out.println(i[4]);
int price[] = new int[100];//100 x 4 = 400 Bytes
price[0] = 10;
price[1] = 20;
//act memory taken = 2 x 4 = 8 Bytes
//waste memory = 400 - 8 = 392 bytes
System.out.println(price[0]);
System.out.println("---------------");
//2. double array:
double d[] = new double[2];
d[0] = 12.33;
d[1] = 100;
for(int k=0; k<d.length; k++) {
System.out.println(d[k]);
}
for(double e : d) {
System.out.println(e);
}
//3. char array:
char c[] = new char[3];
System.out.println("---------------");
//4. String array:
String names[] = new String[3];
names[0] = "Mohit";
names[1] = "Sonam";
names[2] = "Sachin";
for(int t=0; t<names.length; t++) {
System.out.println(names[t]);
}
for(String e : names) {
System.out.println(e);
}
//5. Emp Data : name(String), age(int), gender(char), salary(double), isPerm(boolean)
//Object static Array:
//Object: is a parent class of all the classes in Java
Object empData[] = new Object[5];
empData[0] = "Lisa";
empData[1] = 30;
empData[2] = 'F';
empData[3] = 34.55;
empData[4] = true;
//to print all the EmpData values:
System.out.println("---------------");
for(int n=0; n<empData.length; n++) {
System.out.println(n+ "=" + empData[n]);
}
System.out.println("---------------");
int count = 0;
for(Object e : empData) {
System.out.println(count + "=" + e);
count++;
}
Object carInfo[] = new Object[5];
carInfo[0] = "Swift";
carInfo[1] = "White";
carInfo[2] = 4;
carInfo[3] = false;
carInfo[4] = "Sedan";
Object carInfo1[] = new Object[5];
carInfo[0] = "BMW";
carInfo[1] = "White";
carInfo[2] = 4;
carInfo[3] = false;
carInfo[4] = "Sedan";
}
}