> For the complete documentation index, see [llms.txt](https://gyansetu-selenium.gitbook.io/selenium-testing/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-selenium.gitbook.io/selenium-testing/untitled-2.md).

# Array in Java

&#x20;An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation its length is fixed.&#x20;

It stores the value on the basis of the index value.

#### &#x20;Advantage of Array

&#x20;**One variable can store multiple values**: The main advantage of the array is we can represent multiple values under the same name.&#x20;

**Code Optimization:** No, need to declare a lot of variable of same type data, we can retrieve and sort data easily.&#x20;

**Random access:** We can retrieve any data from array with the help of the index value.&#x20;

#### Disadvantage of Array&#x20;

The main limitation of the array is Size Limit when once we declare array there is no chance to increase and decrease the size of an array according to our requirement, Hence memory point of view array concept is not recommended to use. To overcome this limitation, Java introduces the collection concept.&#x20;

#### Types of Array&#x20;

There are two types of array in Java.

* &#x20;Single Dimensional Array&#x20;
* Multidimensional Array

#### &#x20;Array Declaration&#x20;

String studentName\[]; Or

&#x20;String \[]studentName; Or

&#x20;String\[] studentName;

> &#x20;**Note:** At the time of array declaration we cannot specify the size of the array. For Example int\[5] This is wrong.

#### &#x20;Array creation

&#x20;Every array in a Java is an object, hence we can create array by using new keyword.&#x20;

int\[] arr = new int\[10]; // The size of array is 10.

&#x20;or int\[] arr = {10,20,30,40,50};&#x20;

#### Accessing array elements&#x20;

Access the elements of array by using index value of an element. arrayname\[n-1];

#### &#x20;Java array initialization and instantiation together

&#x20;int marks\[] = {98, 95, 91, 93, 97};

#### &#x20;Iterating a Java Array

![](/files/-LTfGlUyyPJCu9kMZpvG)

#### &#x20;Multidimensional Arrays

&#x20;When a component type itself is an array type, then it is a multidimensional array. Though you can have multiple dimensions nested to ‘n’ level, the final dimension should be a basic type of primitive or an Object.

![](/files/-LTfGyoiWP07OgwF9Fj6)

#### &#x20;Iterate a java multidimensional array

![](/files/-LTfH432goXpMXp9kyc0)

#### &#x20;Sort a Java array

&#x20;java api Arrays contains static methods for sorting. It is a best practice to use them always to sort an array.

![](/files/-LTfHDbfF5WtSVLD74Uj)

#### &#x20;Difference Between Length and Length() in Java

&#x20;**length:** It is a final variable and only applicable for array. It represents size of array

![](/files/-LTfHQcAdP1JIIrKn8un)

&#x20;**length():** It is the final method applicable only for String objects. It represents the number of characters present in the String.

![](/files/-LTfHZT96Fv44kqMrwqM)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-selenium.gitbook.io/selenium-testing/untitled-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
