You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Arrays/Arrays.md
+198Lines changed: 198 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,3 +92,201 @@ Output
92
92
93
93
--------
94
94
95
+
##### For-each Loop for Java Array
96
+
97
+
We can also print the Java array using for-each loop. The Java for-each loop prints the array elements one by one. It holds an array element in a variable, then executes the body of the loop.
98
+
99
+
The syntax of the for-each loop is given below:
100
+
```java
101
+
for(data_type variable:array){
102
+
//body of the loop
103
+
}
104
+
```
105
+
Let us see the example of print the elements of Java array using the for-each loop.
106
+
```java
107
+
classTestarray1{
108
+
publicstaticvoidmain(Stringargs[]){
109
+
int arr[]={33,3,4,5};
110
+
//printing array using for-each loop
111
+
for(int i:arr)
112
+
System.out.println(i);
113
+
}
114
+
}
115
+
```
116
+
Output
117
+
```
118
+
33
119
+
3
120
+
4
121
+
5
122
+
```
123
+
124
+
------
125
+
126
+
##### Passing Array to a Method in Java
127
+
```java
128
+
//Java Program to demonstrate the way of passing an array
129
+
//to method.
130
+
classTestarray2{
131
+
//creating a method which receives an array as a parameter
132
+
staticvoidmin(intarr[]){
133
+
int min=arr[0];
134
+
for(int i=1;i<arr.length;i++)
135
+
if(min>arr[i])
136
+
min=arr[i];
137
+
138
+
System.out.println(min);
139
+
}
140
+
141
+
publicstaticvoidmain(Stringargs[]){
142
+
int a[]={33,3,4,5};//declaring and initializing an array
143
+
min(a);//passing array to method
144
+
}
145
+
}
146
+
```
147
+
Output
148
+
```
149
+
3
150
+
```
151
+
152
+
153
+
------
154
+
155
+
##### Anonymous Array in Java
156
+
157
+
Java supports the feature of an anonymous array, so you don't need to declare the array while passing an array to the method.
158
+
159
+
```java
160
+
//Java Program to demonstrate the way of passing an anonymous array
161
+
//to method.
162
+
publicclassTestAnonymousArray{
163
+
//creating a method which receives an array as a parameter
164
+
staticvoidprintArray(intarr[]){
165
+
for(int i=0;i<arr.length;i++)
166
+
System.out.println(arr[i]);
167
+
}
168
+
169
+
publicstaticvoidmain(Stringargs[]){
170
+
printArray(newint[]{10,22,44,66});//passing anonymous array to method
171
+
}
172
+
}
173
+
```
174
+
175
+
Output
176
+
```
177
+
10
178
+
22
179
+
44
180
+
66
181
+
```
182
+
183
+
------
184
+
185
+
##### Returning Array from the Method
186
+
187
+
```java
188
+
//Java Program to return an array from the method
189
+
classTestReturnArray{
190
+
//creating method which returns an array
191
+
staticint[] get(){
192
+
returnnewint[]{10,30,50,90,60};
193
+
}
194
+
195
+
publicstaticvoidmain(Stringargs[]){
196
+
//calling method which returns an array
197
+
int arr[]=get();
198
+
//printing the values of an array
199
+
for(int i=0;i<arr.length;i++)
200
+
System.out.println(arr[i]);
201
+
}}
202
+
```
203
+
Output
204
+
```
205
+
10
206
+
30
207
+
50
208
+
90
209
+
60
210
+
```
211
+
212
+
-----
213
+
214
+
##### ArrayIndexOutOfBoundsException
215
+
216
+
```java
217
+
//Java Program to demonstrate the case of
218
+
//ArrayIndexOutOfBoundsException in a Java Array.
219
+
publicclassTestArrayException{
220
+
publicstaticvoidmain(Stringargs[]){
221
+
int arr[]={50,60,70,80};
222
+
for(int i=0;i<=arr.length;i++){
223
+
System.out.println(arr[i]);
224
+
}
225
+
}
226
+
}
227
+
```
228
+
Output
229
+
```
230
+
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
231
+
at TestArrayException.main(TestArrayException.java:5)
232
+
50
233
+
60
234
+
70
235
+
80
236
+
```
237
+
238
+
#### Multidimensional Array in Java
239
+
240
+
In such case, data is stored in row and column based index (also known as matrix form).
241
+
242
+
Syntax to Declare Multidimensional Array in Java
243
+
244
+
```
245
+
dataType[][] arrayRefVar; (or)
246
+
dataType [][]arrayRefVar; (or)
247
+
dataType arrayRefVar[][]; (or)
248
+
dataType []arrayRefVar[];
249
+
```
250
+
251
+
```java
252
+
int[][] arr=newint[3][3];//3 row and 3 column
253
+
```
254
+
Example to initialize Multidimensional Array in Java
255
+
```java
256
+
arr[0][0]=1;
257
+
arr[0][1]=2;
258
+
arr[0][2]=3;
259
+
arr[1][0]=4;
260
+
arr[1][1]=5;
261
+
arr[1][2]=6;
262
+
arr[2][0]=7;
263
+
arr[2][1]=8;
264
+
arr[2][2]=9;
265
+
```
266
+
267
+
#### Example of Multidimensional Java Array
268
+
```java
269
+
//Java Program to illustrate the use of multidimensional array
0 commit comments