Skip to content

Commit 7c2b536

Browse files
authored
Merge pull request #583 from javaistic/add-arrays-docs
Add arrays docs
2 parents 201042a + 48d6762 commit 7c2b536

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/navs/documentation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ export const documentationNav = {
2727
pages['break-statement'],
2828
pages['continue-statement'],
2929
],
30+
'Java Arrays': [pages['arrays']],
3031
}

src/pages/docs/arrays.mdx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Java Arrays
3+
description: In this tutorial, we will learn how to use the Java continue statement to skip the current iteration of a loop.
4+
---
5+
6+
## Java Arrays
7+
An array is a collection of similar types of data.
8+
9+
For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names.
10+
11+
```java
12+
String[] array = new String[100];
13+
```
14+
Here, the above array cannot store more than 100 names. The number of values in a Java array is always fixed.
15+
16+
### How to declare an array in Java?
17+
In Java, here is how we can declare an array.
18+
19+
```java
20+
dataType[] arrayName;
21+
```
22+
- `dataType` - it can be primitive data types like `int`, `char`, `double`, `byte`, etc. or Java objects
23+
- `arrayName` - it is an identifier
24+
For example,
25+
26+
```java
27+
double[] data;
28+
```
29+
Here, data is an array that can hold values of type double.
30+
31+
But, how many elements can array this hold?
32+
33+
Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example,
34+
35+
```java
36+
// declare an array
37+
double[] data;
38+
39+
// allocate memory
40+
data = new double[10];
41+
```
42+
Here, the array can store 10 elements. We can also say that the size or length of the array is 10.
43+
44+
In Java, we can declare and allocate the memory of an array in one single statement. For example,
45+
46+
```java
47+
double[] data = new double[10];
48+
```
49+
### How to Initialize Arrays in Java?
50+
In Java, we can initialize arrays during declaration. For example,
51+
52+
```java
53+
//declare and initialize and array
54+
int[] age = {12, 4, 5, 2, 5};
55+
```
56+
Here, we have created an array named age and initialized it with the values inside the curly brackets.
57+
58+
Note that we have not provided the size of the array. In this case, the Java compiler automatically specifies the size by counting the number of elements in the array (i.e. 5).
59+
60+
In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number. For example,
61+
62+
```java
63+
// declare an array
64+
int[] age = new int[5];
65+
66+
// initialize array
67+
age[0] = 12;
68+
age[1] = 4;
69+
age[2] = 5;
70+
..
71+
```
72+
Elements are stored in the array
73+
Java Arrays initialization
74+
75+
Note:
76+
77+
Array indices always start from 0. That is, the first element of an array is at index 0.
78+
If the size of an array is n, then the last element of the array will be at index n-1.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy