Unverified Commit 25d33140 authored by Lokesh Singh's avatar Lokesh Singh Committed by GitHub

Update readme.md

parent 99f3f55e
# Array
In java arrays are obkects.
1. Arrays in Java work differently than they do in C/C++.
A Java array variable can also be declared like other variables with [] after the data type.
......@@ -27,10 +28,32 @@ example:
int myArray[];
```
intializing an array
#### intializing an array
In Single Line
```
var-name = new type[size];
int [] arr= {1,2,3,4,5,6,7};
```
Using loop
An array can be initialized using a loop.
```java
import java.io.*;
import java.util.*;
public class Example {
public static void main(String[] args) {
int[] arr = new int[5];
Scanner Scan = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = Scan.nextInt();
}
}
}
Input: 2 3 4 5 6
```
example:
```
int myArray[];
......@@ -61,6 +84,24 @@ for(int i = 0; i<intArray.length; i++) {
}
```
using FOR-EACH loop
This is a special type of loop to access array elements of the array. But this loop can be used only to traverse an array, and nothing can be changed in the array using this loop.
```java
import java.io.*;
import java.util.*;
public class Solutions {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
for (int i: arr) {
System.out.print(i + " ");
}
}
}
Output:
10 20 30 40 50
```
## Array of Object
An array of objects is created like an array of primitive type data items in the following way.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment