Skip to content

Commit 7bff496

Browse files
authored
GH-233: Array in Java (#234)
1 parent ea432cb commit 7bff496

File tree

4 files changed

+389
-3
lines changed

4 files changed

+389
-3
lines changed

concepts/general/array/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
*See implementation in*
44
[C++](/concepts/cpp/array/README.md),
5-
Java,
5+
[Java](/concepts/java/array/README.md),
66
[Python](/concepts/python/array/README.md),
77
[Typescript](/concepts/typescript/array/README.md)
88

@@ -22,9 +22,9 @@ Here are some basic operations that you can perform on arrays:
2222
* Initialization: Create an array with given values
2323
* Accessing Elements: Access an element by its index (zero-based)
2424
* Updating Elements: Update an element by its index
25+
* Size: Get the number of elements in the array
2526
* Traversal: Visit all elements of the array
2627
* Input: Read values into the array from a stream
27-
* Size: Get the number of elements in the array
2828

2929
More operations:
3030

concepts/java/array/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Array in Java
2+
3+
In computer science, an `array` is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula.
4+
5+
## Time Complexity Analysis of Array
6+
7+
| Operation | Creation | Insertion | Accessing | Update | Deletion | Searching |
8+
|-----------------|----------|------------------|-----------|--------|------------------|-----------|
9+
| Time Complexity | $O(1)$ | $O(1)$ or $O(n)$ | $O(1)$ | $O(n)$ | $O(1)$ or $O(n)$ | $O(n)$ |
10+
11+
## Array implementation in Java
12+
13+
## Basic Operations
14+
15+
**Initialization**: Create an array with given values
16+
17+
18+
```Java
19+
int a[] = new int[5];
20+
21+
a[0] = 10;
22+
a[1] = 20;
23+
a[2] = 30;
24+
a[3] = 40;
25+
```
26+
27+
28+
29+
30+
40
31+
32+
33+
34+
35+
```Java
36+
int b[] = {1, 2, 3, 4, 5};
37+
```
38+
39+
**Accessing Elements**: Access element by index (zero-based)
40+
41+
42+
```Java
43+
System.out.println("a[0] = " + a[0]);
44+
System.out.println("a[4] = " + a[4]);
45+
```
46+
47+
a[0] = 10
48+
a[4] = 0
49+
50+
51+
**Updating Elements**: Update an element by its index
52+
53+
54+
```Java
55+
a[0] = 100;
56+
System.out.println("a[0] = " + a[0]);
57+
```
58+
59+
a[0] = 100
60+
61+
62+
**Size**: Get the number of elements in the array
63+
64+
You can use the [`length`](https://docs.oracle.com/javase/specs/jls/se19/html/jls-10.html#jls-10.7) property of the array to get the number of elements in the array.
65+
66+
67+
```Java
68+
System.out.println("a.length = " + a.length);
69+
```
70+
71+
a.length = 5
72+
73+
74+
**Traversal**: Visit all elements of the array
75+
76+
77+
```Java
78+
for(int i=0; i<a.length; i++) {
79+
System.out.println("a[" + i + "] = " + a[i]);
80+
}
81+
```
82+
83+
a[0] = 100
84+
a[1] = 20
85+
a[2] = 30
86+
a[3] = 40
87+
a[4] = 0
88+
89+
90+
**Input**: Read values into the array from a stream
91+
92+
```java
93+
import java.util.Scanner;
94+
95+
public class Main {
96+
public static void main(String[] args)
97+
{
98+
Scanner s = new Scanner(System.in);
99+
int n = s.nextInt();
100+
int a[] = new int[n];
101+
for(int i=0; i<n; i++){
102+
a[i] = s.nextInt();
103+
}
104+
105+
for(int i=0; i<n; i++){
106+
System.out.print(a[i] + " ");
107+
}
108+
}
109+
}
110+
```
111+
112+
## 🔗 Further Reading
113+
114+
* [Array (data structure)](https://en.wikipedia.org/wiki/Array_(data_structure))
115+
* ▶️ [Array In Data Structure](https://www.youtube.com/watch?v=eXFItikqw8c&ab_channel=Simplilearn), Simplilearn
116+
* ▶️ [Dynamic and Static Arrays](https://www.youtube.com/watch?v=PEnFFiQe1pM&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=4&ab_channel=WilliamFiset), WilliamFiset
117+
* ▶️ [An Overview of Arrays and Memory (Data Structures & Algorithms #2)](https://www.youtube.com/watch?v=pmN9ExDf3yQ&ab_channel=CSDojo), CS Dojo
118+

0 commit comments

Comments
 (0)