Skip to content

Commit 20ab751

Browse files
committed
added arrays
1 parent 3d6cebf commit 20ab751

File tree

2 files changed

+199
-2
lines changed

2 files changed

+199
-2
lines changed

.idea/workspace.xml

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Arrays/Arrays.md

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,201 @@ Output
9292

9393
--------
9494

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+
class Testarray1{
108+
public static void main(String args[]){
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+
class Testarray2{
131+
//creating a method which receives an array as a parameter
132+
static void min(int arr[]){
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+
public static void main(String args[]){
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+
public class TestAnonymousArray{
163+
//creating a method which receives an array as a parameter
164+
static void printArray(int arr[]){
165+
for(int i=0;i<arr.length;i++)
166+
System.out.println(arr[i]);
167+
}
168+
169+
public static void main(String args[]){
170+
printArray(new int[]{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+
class TestReturnArray{
190+
//creating method which returns an array
191+
static int[] get(){
192+
return new int[]{10,30,50,90,60};
193+
}
194+
195+
public static void main(String args[]){
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+
public class TestArrayException{
220+
public static void main(String args[]){
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=new int[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
270+
class Testarray3{
271+
public static void main(String args[]){
272+
//declaring and initializing 2D array
273+
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
274+
//printing 2D array
275+
for(int i=0;i<3;i++){
276+
for(int j=0;j<3;j++){
277+
System.out.print(arr[i][j]+" ");
278+
}
279+
System.out.println();
280+
}
281+
}
282+
}
283+
```
284+
Output
285+
```
286+
1 2 3
287+
2 4 5
288+
4 4 5
289+
290+
```
291+
292+

0 commit comments

Comments
 (0)