---
title: "How to create and work with arrays in Java?"  
description: "How to create and work with arrays in Java?"  
author: "Sandra Emily"  
published: 2023-09-07  
updated: 2023-09-09  
canonical: https://www.mindstick.com/forum/159862/how-to-create-and-work-with-arrays-in-java  
category: "java"  
tags: ["java", "array"]  
reading_time: 3 minutes  

---

# How to create and work with arrays in Java?

How can I create and work with [arrays](https://www.mindstick.com/articles/11955/arrays-and-its-limitations) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

In Java, an array is a data structure that can store a collection of data of the same type. Each element in the array has a unique index, starting from 0.

To create an array in Java, you need to use the `new` keyword. The syntax to create an array is:

```plaintext
datatype[] arrayName = new datatype[length];
```

where `datatype` is the type of data the array will store, `arrayName` is the name of the array, and `length` is the number of elements the array will contain.

For example, the following code creates an array of integers named `numbers` with 5 elements:

```plaintext
int[] numbers = new int[5];
```

You can also initialize an array while creating it. To do this, you can use the curly braces to enclose the initial values of the array elements. For example, the following code creates a string of strings named `names` with 3 elements and initializes the first element to "John", the second element to "Jane", and the third element to "Peter":

```plaintext
String[] names = new String[] {"John", "Jane", "Peter"};
```

Once you have created an array, you can access its elements using their indexes. The index of the first element is 0, the index of the second element is 1, and so on.

To access an element in an array, you can use the following syntax:

```plaintext
arrayName[index];
```

For example, the following code prints the value of the first element of the `numbers` array:

```plaintext
System.out.println(numbers[0]);
```

You can also change the value of an element in an array. To do this, you can use the following syntax:

```plaintext
arrayName[index] = value;
```

For example, the following code changes the value of the first element in the `numbers` array to 10:

```plaintext
numbers[0] = 10;
```

You can also iterate through the elements of an array using a for loop. The following code loops through the elements of the `numbers` array and prints each element to the console:

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

Here are some other things to keep in mind when working with arrays in Java:

- The size of the table cannot be changed after it is created.
- If you try to access an array element that is out of bounds, a `ArrayIndexOutOfBoundsException` will be thrown.
- You can use the `length` property of an array to know its size.


---

Original Source: https://www.mindstick.com/forum/159862/how-to-create-and-work-with-arrays-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
