Java Arrays

To declare an array of integers, you start with:

int[] myArray;

To instantiate an array of ten integers, you can try:

myArray = new int[10];

To set values in that array, try:

myArray[0] = 1; // arrays indices are 0 based in Java

Or at instantiation:

int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

To get values from the array, try:

System.out.println(myArray[0]);

To print all the values in an array, try:

// go from 0 to one less than the array length, based on 0 indexing
for(int i = 0; i < myArray2.length; i++) {
    System.out.println(myArray2[i]);
}

For more information, the tutorial from Sun/Oracle will be of great help. You can also check out the Java language specification on Arrays.

Using the Arrays Utility Class

java.util.Arrays contains a bunch of static methods. Static methods belong to the class and do not require an instance of the class in order to be called. Instead they are called with the class name as a prefix.

So you can do things like the following:

// print a string representation of an array
int[] myArray = {1, 2, 3, 4};
System.out.println(Arrays.toString(myArray));

Or

// sort a list
int[] unsorted = {3, 4, 1, 2, 5, 7, 6};
Arrays.sort(unsorted);
Answer from justkt on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... This class contains various methods for manipulating arrays (such as sorting and searching).
Top answer
1 of 5
17

Java Arrays

To declare an array of integers, you start with:

int[] myArray;

To instantiate an array of ten integers, you can try:

myArray = new int[10];

To set values in that array, try:

myArray[0] = 1; // arrays indices are 0 based in Java

Or at instantiation:

int[] myArray2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

To get values from the array, try:

System.out.println(myArray[0]);

To print all the values in an array, try:

// go from 0 to one less than the array length, based on 0 indexing
for(int i = 0; i < myArray2.length; i++) {
    System.out.println(myArray2[i]);
}

For more information, the tutorial from Sun/Oracle will be of great help. You can also check out the Java language specification on Arrays.

Using the Arrays Utility Class

java.util.Arrays contains a bunch of static methods. Static methods belong to the class and do not require an instance of the class in order to be called. Instead they are called with the class name as a prefix.

So you can do things like the following:

// print a string representation of an array
int[] myArray = {1, 2, 3, 4};
System.out.println(Arrays.toString(myArray));

Or

// sort a list
int[] unsorted = {3, 4, 1, 2, 5, 7, 6};
Arrays.sort(unsorted);
2 of 5
8

Well let's say you have an array

int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};

And you want to sort it. You do this:

// assumes you imported at the top
Arrays.sort(myArray);

Here's the whole shebang:

import java.util.Arrays;
class ArrayTest {
    public static void main(String[] args) {
        int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
        Arrays.sort(myArray);
        System.out.println(Arrays.toString(myArray));
    }
}

And that results in

C:\Documents and Settings\glow\My Documents>java ArrayTest
[1, 2, 3, 4, 6, 8, 9]

C:\Documents and Settings\glow\My Documents>
🌐
GeeksforGeeks
geeksforgeeks.org › java › array-class-in-java
Arrays Class in Java - GeeksforGeeks
... import java.util.Arrays; class Geeks{ public static void main(String[] args){ // Get the Array int intArr[] = { 10, 20, 15, 22, 35 }; // To convert the elements as List System.out.println("int Array as List: " + Arrays.asList(intArr)); } }
Published   May 16, 2026
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
How Tos Add Two Numbers Swap Two ... Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String ...
🌐
Pressbooks
pressbooks.pub › javaprogramming › chapter › arrays-java-programming
Arrays — Java Programming – Java Programming
Program 4.3 Program to sort an array of randomly obtained using the Math.random() method. //sortArray.java Generate a set of 10 random numbers and sort them. //Click here for code import java.io.*; import java.util.*; public class sortArray { public static void main (String args[]) { //create an array int[] a = new int[10]; int i; for (i = 0; i < a.length; i++) a[i] = (int) (Math.random () * 100); //generate random numbers sort (a); for (i = 0; i < a.length; i++) System.out.println (a[i]); } public static void sort (int[]a) { //sort the array int n = a.length; int i, j, k; boolean flag; flag = false; for (i = 0; i < n; i++) { for (j = 0; j < n - 1; j++) { if (a[j] > a[j + 1]) { k = a[j]; a[j] = a[j + 1]; a[j + 1] = k; flag = true; } } if (!flag) break; } } } /* Output: 1 27 33 41 45 54 62 75 87 95 */ All programming languages support arrays.
🌐
Java and OOP
skeoop.github.io › java-basics › 18-Using-Arrays
Using Arrays | Java and OOP
The Arrays class in package java.util contains static methods for array operations. You should use this class instead of writing the methods yourself! import java.util.Arrays; .
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › util › Arrays.java
jdk/src/java.base/share/classes/java/util/Arrays.java at master · openjdk/jdk
import java.lang.reflect.Array; import java.util.concurrent.ForkJoinPool; import java.util.function.BinaryOperator; import java.util.function.Consumer; import java.util.function.DoubleBinaryOperator; import java.util.function.IntBinaryOperator; import java.util.function.IntFunction; import java.util.function.IntToDoubleFunction; import java.util.function.IntToLongFunction; import java.util.function.IntUnaryOperator; import java.util.function.LongBinaryOperator; import java.util.function.UnaryOperator; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; import java.util.stream.Stream; import java.util.stream.StreamSupport; ·
Author   openjdk
Find elsewhere
🌐
Reddit
reddit.com › r/programminghelp › why cant arrays be resolved with import java.utils.*;
r/programminghelp on Reddit: Why cant Arrays be resolved with import java.utils.*;
October 28, 2021 -

Hello everyone, my code is below. For some reason I cannot run it. I'm using drjava as that is what's required for my class. Error details below code. Thanks so much in advance. I'm super grateful for this sub.

Code:

===================================================================

import java.util.*;

public class Main{

public static void main(String[] args){

int[] list = {18, 7, 4, 14, 11};

int[] list2 = stretch(list);

System.out.println(Arrays.toString(list)); // [18, 7, 4, 24, 11]

System.out.println(Arrays.toString(list2)); // [9, 9, 4, 3, 2, 2, 7, 7, 6, 5]

}

public static int[] stretch(int[] arr){

int[] arr1 = {};

for (int i = 0; i < arr.length; i++){

for (int j = 0; j < arr.length * 2; i++){

if (arr[i] % 2 == 0){

arr1[j] = arr[i] / 2;

arr1[j + 1] = arr[i] / 2;

} else {

arr1[j] = arr[i] / 2 + 1;

arr1[j + 1] = arr[i] / 2;

}

return arr;

}

}

}

}

========================================================================

Errors:

==========================================================================

3 errors and 1 warning found:

--------------

*** Errors ***

--------------

File: C:\Users\usr\Main.java [line: 7]

Error: Arrays cannot be resolved

File: C:\Users\usr\Main.java [line: 8]

Error: Arrays cannot be resolved

File: C:\Users\usr\Main.java [line: 12]

Error: This method must return a result of type int[]

-------------

** Warning **

-------------

File: C:\Users\usr\Main.java [line: 15]

Warning: Dead code

======================================================================

🌐
Naveenpn
blog.naveenpn.com › arrays-class-in-java
Java Arrays Class Guide
April 15, 2025 - ... import java.util.Arrays; public class ArrayToString { public static void main(String[] args) { String[] fruits = {"Apple", "Banana", "Cherry"}; System.out.println(Arrays.toString(fruits)); // Output: [Apple, Banana, Cherry] } }
🌐
Scaler
scaler.com › home › topics › java › arrays class in java
Arrays Class in Java - Scaler Topics
May 5, 2024 - This article explains the array of utilities in Java and the methods of the Arrays Class in Java. It also covers the benefits and implementation of the array class.
🌐
Octoperf
blog.octoperf.com › java-arrays
Java Arrays - OctoPerf
March 20, 2018 - Here we have an array of Person, ... class in Java. (even if not specified in the code) ... I have created an array with 2 memory spaces. Then, I have assigned 2 Person instances, to index 0 and index 1 respectively. In the case you try to access an index which is outside the array size, the code will throw an ArrayIndexOutOfBoundsException. import org.junit.Test; ...
🌐
Scribd
scribd.com › document › 863043984 › K-Scheme-Java-IMP-Questions-by-VJTech-Academy
Java Important Questions and Answers | PDF
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
CodeSignal
codesignal.com › learn › courses › mastering-complex-data-structures-in-java › lessons › understanding-and-using-arrays-in-java
Understanding and Using Arrays in Java
Indexes in Java arrays are zero-based. For example, the first element has an index of 0. The length property of an array returns the number of elements present in the array and can be used to easily determine its size or iterate over the array. Consider the following example that depicts inspecting and modifying arrays, as well as demonstrating the length property: import java.util.Arrays; class ArrayExample { public void inspectArray() { String[] myArray = {"apple", "banana", "cherry", "durian", "elderberry"}; System.out.println(myArray[1]); // Output: banana System.out.println(myArray[myArra
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
For other primitive types, use: Boolean for boolean, Character for char, Double for double, etc: Create an ArrayList to store numbers (add elements of type Integer): import java.util.ArrayList; public class Main { public static void main(String[] ...
🌐
Rutgers CS
cs.rutgers.edu › courses › 111 › classes › fall_2011_tjang › texts › notes-java › data › arrays › arrays-library.html
Java: Array Library Methods
USE java.util.Arrays.toString(...) and you'll get something much more readable. An import java.util.*; statement avoids the extra package qualifier.
🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-aslist-method-explained-b308fac8f6fc
Java’s Arrays.asList() Method Explained | Medium
July 17, 2024 - Here’s a basic example that demonstrates converting an array of strings into a list: import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { String[] array = {"Apple", "Banana", "Cherry"}; ...
🌐
Stack Overflow
stackoverflow.com
Newest Questions - Stack Overflow
Let's say I have a bash array that looks like this: samples=("one 1 uno" "two 2 dos" "three 3 tres") I'm looping through this array, hoping that I will get three ...
🌐
Baeldung
baeldung.com › home › java › java array › array operations in java
Array Operations in Java | Baeldung
January 8, 2024 - Both Arrays and ArrayUtils classes ship with their implementations to convert the data structures to a readable String. Apart from the slightly different format they use, the most important distinction is how they treat multi-dimensional objects. The Java Util’s class provides two static methods we can use: