Skip to content

Commit cadbd2c

Browse files
authored
[Edit] Java Arrays: .length (#6607)
1 parent 3ec88f7 commit cadbd2c

File tree

1 file changed

+96
-13
lines changed
  • content/java/concepts/arrays/terms/length

1 file changed

+96
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,126 @@
11
---
22
Title: '.length'
3-
Description: 'The .length property is used to determine the length or size of an array.'
3+
Description: 'Determines the length or size of an array.'
44
Subjects:
55
- 'Code Foundations'
66
- 'Computer Science'
77
Tags:
8-
- 'Methods'
98
- 'Arrays'
9+
- 'Elements'
10+
- 'Properties'
11+
- 'Values'
1012
CatalogContent:
1113
- 'learn-java'
1214
- 'paths/computer-science'
1315
---
1416

15-
In Java, the **`.length`** property is used to determine the length or size of an array. It is a built-in property for arrays and returns an integer value that represents the number of elements in the array.
17+
In Java, the **`.length`** property is used to determine the length or size of an [array](https://www.codecademy.com/resources/docs/java/arrays). It is a built-in property for arrays and returns an integer value that represents the number of elements in the array. Understanding how to use `.length` is crucial for iterating through arrays, avoiding out-of-bounds [errors](https://www.codecademy.com/resources/docs/java/errors), and dynamically managing data.
1618

1719
## Syntax
1820

19-
The syntax of the `.length` property is the following:
20-
2121
```pseudo
2222
arrayName.length;
2323
```
2424

25+
**Parameters:**
26+
2527
- `arrayName`: The name of the array to call `.length` on.
2628

27-
## Example
29+
**Return Value:**
30+
31+
The `.length` property returns an integer value that represents the number of elements in the array.
2832

29-
The example below uses the `.length` property to determine the size of an array:
33+
## Example 1: Basic Usage of `.length`
34+
35+
This example uses the `.length` property to determine the array length:
3036

3137
```java
32-
int[] numbers = {1, 2, 3, 4, 5};
33-
int length = numbers.length;
34-
System.out.println("Length of the array is" + length)
38+
public class Array {
39+
public static void main(String[] args) {
40+
// Create an array
41+
String[] colors = {"Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"};
42+
43+
// Find the size of the array
44+
int length = colors.length;
45+
46+
// Print the result
47+
System.out.println("The length of the array is: " + length);
48+
}
49+
}
3550
```
3651

37-
In this example, the `numbers` array contains five elements, and the `.length` property returns the value `5`, which is assigned to the `length` variable.
52+
Since the `colors` array contains 7 elements, the output for the example will be:
53+
54+
```shell
55+
The length of the array is: 7
56+
```
3857

39-
Output for the above code will be:
58+
## Example 2: Using `.length` for Looping Through an Array
59+
60+
This example uses the `.length` property for [looping](https://www.codecademy.com/resources/docs/java/loops) through an array and printing the elements in it:
61+
62+
```java
63+
public class Loop {
64+
public static void main(String[] args) {
65+
// Create an array
66+
String[] fruits = {"Apple", "Banana", "Cherry"};
67+
68+
// Loop through the array and print the elements in it
69+
for (int i = 0; i < fruits.length; i++) {
70+
System.out.println(fruits[i]);
71+
}
72+
}
73+
}
74+
```
75+
76+
The output for the example will be:
4077

4178
```shell
42-
Length of the array is 5
79+
Apple
80+
Banana
81+
Cherry
4382
```
83+
84+
## Example 3: Using `.length` with Multi-Dimensional Arrays
85+
86+
This example uses the `.length` property to determine the number of rows and number of columns in the first row of a multi-dimensional array:
87+
88+
```java
89+
public class MultiDimensionalArray {
90+
public static void main(String[] args) {
91+
// Create a multi-dimensional array
92+
int[][] numbers = {
93+
{1, 2, 3},
94+
{4, 5, 6},
95+
{7, 8, 9}
96+
};
97+
98+
// Print the number of rows in the array
99+
System.out.println("The number of rows in the array: " + numbers.length);
100+
101+
// Print the number of columns in the first row of the array
102+
System.out.println("The number of columns in the first row of the array: " + numbers[0].length);
103+
}
104+
}
105+
```
106+
107+
The output for the example will be:
108+
109+
```shell
110+
The number of rows in the array: 3
111+
The number of columns in the first row of the array: 3
112+
```
113+
114+
## Frequently Asked Questions
115+
116+
### 1. What is the difference between `.length` and `.length()`?
117+
118+
`.length` is an array property, whereas [`.length()`](https://www.codecademy.com/resources/docs/java/strings/length) is a [string](https://www.codecademy.com/resources/docs/java/strings) method.
119+
120+
### 2. Can the length of an array be changed?
121+
122+
No. Once an array is created, its length is constant and cannot be changed. To work with dynamic sizes, consider using [`ArrayList`](https://www.codecademy.com/resources/docs/java/array-list).
123+
124+
### 3. What happens if I access an index beyond the array's length?
125+
126+
You'll get an [`ArrayIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/java/errors/arrayindexoutofboundsexception) error, which is a runtime error.

0 commit comments

Comments
 (0)