Skip to content

GH-233: Array in Java #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions concepts/general/array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

*See implementation in*
[C++](/concepts/cpp/array/README.md),
Java,
[Java](/concepts/java/array/README.md),
[Python](/concepts/python/array/README.md),
[Typescript](/concepts/typescript/array/README.md)

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

More operations:

Expand Down
118 changes: 118 additions & 0 deletions concepts/java/array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Array in Java

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.

## Time Complexity Analysis of Array

| Operation | Creation | Insertion | Accessing | Update | Deletion | Searching |
|-----------------|----------|------------------|-----------|--------|------------------|-----------|
| Time Complexity | $O(1)$ | $O(1)$ or $O(n)$ | $O(1)$ | $O(n)$ | $O(1)$ or $O(n)$ | $O(n)$ |

## Array implementation in Java

## Basic Operations

**Initialization**: Create an array with given values


```Java
int a[] = new int[5];

a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
```




40




```Java
int b[] = {1, 2, 3, 4, 5};
```

**Accessing Elements**: Access element by index (zero-based)


```Java
System.out.println("a[0] = " + a[0]);
System.out.println("a[4] = " + a[4]);
```

a[0] = 10
a[4] = 0


**Updating Elements**: Update an element by its index


```Java
a[0] = 100;
System.out.println("a[0] = " + a[0]);
```

a[0] = 100


**Size**: Get the number of elements in the array

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.


```Java
System.out.println("a.length = " + a.length);
```

a.length = 5


**Traversal**: Visit all elements of the array


```Java
for(int i=0; i<a.length; i++) {
System.out.println("a[" + i + "] = " + a[i]);
}
```

a[0] = 100
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 0


**Input**: Read values into the array from a stream

```java
import java.util.Scanner;

public class Main {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int a[] = new int[n];
for(int i=0; i<n; i++){
a[i] = s.nextInt();
}

for(int i=0; i<n; i++){
System.out.print(a[i] + " ");
}
}
}
```

## 🔗 Further Reading

* [Array (data structure)](https://en.wikipedia.org/wiki/Array_(data_structure))
* ▶️ [Array In Data Structure](https://www.youtube.com/watch?v=eXFItikqw8c&ab_channel=Simplilearn), Simplilearn
* ▶️ [Dynamic and Static Arrays](https://www.youtube.com/watch?v=PEnFFiQe1pM&list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu&index=4&ab_channel=WilliamFiset), WilliamFiset
* ▶️ [An Overview of Arrays and Memory (Data Structures & Algorithms #2)](https://www.youtube.com/watch?v=pmN9ExDf3yQ&ab_channel=CSDojo), CS Dojo

Loading