This is correct.

A[] a = new A[4];

...creates 4 A references, similar to doing this:

A a1;
A a2;
A a3;
A a4;

Now you couldn't do a1.someMethod() without allocating a1 like this:

a1 = new A();

Similarly, with the array you need to do this:

a[0] = new A();

...before using it.

Answer from MeBigFatGuy on Stack Overflow
🌐
Medium
medium.com › @manisha10850 › array-in-java-d5d8b2357c1a
Array in Java. What is an Array? | by Manisha Shukla | Medium
May 27, 2025 - Java provides a data structure that stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the ...
🌐
Scaler
scaler.com › home › topics › java › java arrays
Understanding Array in Java - Scaler Topics
April 8, 2024 - Arrays in Java can hold primitive data types (Integer, Character, Float, etc.) and non-primitive data types(Object). The values of primitive data types are stored in a memory location, whereas in the case of objects, it's stored in heap memory. Hence, it’s required to specify the data type while creating it.
Discussions

How do I determine whether an array contains a particular value in Java? - Stack Overflow
Odd, NetBeans claims that ... the Contains doesn't work since it just has one element; the int array. 2010-11-13T13:15:59.703Z+00:00 ... Nyerguds: indeed, this does not work for primitives. In java primitive types can't be generic.... More on stackoverflow.com
🌐 stackoverflow.com
How to make an array of arrays in Java - Stack Overflow
What I have done is a small utility class Array2D which encapsulates some basic methods such as exixts(...) etc. I posted this below. 2012-11-29T13:54:33.883Z+00:00 ... there is the class I mentioned in the comment we had with Sean Patrick Floyd : I did it with a peculiar use which needs WeakReference, but you can change it by any object with ease. ... import java... More on stackoverflow.com
🌐 stackoverflow.com
[JAVA] Arrays. ELI5 please.
Think of an array kind of like the cabinets in your kitchen, all in a line. Each cabinet has a specific location in your kitchen. If you were to tell someone where to find the cups when they're over at your house, you might say "They're in the cabinet over the stove, on the left". Your cabinets are used for storage of your dishes and other kitchen items, and they can be organized a certain way to make finding what you need easier. A one dimensional array is essentially a line of "cabinets" that you can store data in, where each cabinet has its own index, or location, that you reference it by. An array can be unsorted or sorted, and many algorithms exist to sort them, with some being fast, and some requiring very little extra memory. Something like .length or .size simply returns the amount of cabinets in your kitchen, or in programming terms, the number of spaces in your array. You can also make a two dimensional array, which you can think of like a grid of cubby holes at a pre-school, or more mathematically, as a matrix. These work exactly the same as a 1D array, but each element has two indexes instead of one, like an X and Y coordinate on graph paper. An array is stored linearly in memory and its indexes usually start at zero. The following example is how to create an array and fill it with numbers using Java. int[] myArray = new int[10]; //Creates an array of size 10 to store integers for(int i = 0; i < myArray.length; i++) { myArray[i] = i; } //myArray.length = 10, i = 0, loop until i > 9 //On each iteration, myArray at index i will contain the current value of i //When this loop finishes, myArray will contain 0,1,2,3,4,5,6,7,8,9 in that order Arrays are the simplest data structure you will be dealing with, but they are incredibly useful and have a huge amount of applications. Many complex data structures use arrays in some way. For example, you can represent a graph structure as a matrix, or two dimensional array, of connected edges. You can efficiently sort data without using any extra memory using a heap structure, which at its most basic form is just an array that can be sorted in place. Once you grasp arrays, you will be able to grasp the more complex structures much more easily. If there is anything else I can help you with, please let me know. More on reddit.com
🌐 r/learnprogramming
14
3
May 13, 2013
Java Arrays
Arrays are just a convenient way of storing multiple things. Think of arrays as sacks of balls - you could have just a single ball: int myBall = 42; Or five: int myBall1 = 42; int myBall2 = 43; int myBall3 = 44; int myBall4 = 45; int myBall5 = 46; But what if you want, say, a hundred balls? Things get out of hand rather quickly. Instead of having these loose balls lying around, we can have an array of balls: int[] balls1 = new int[100]; // Holds a hundred balls int[] balls2 = new int[5]; // Holds five balls But wait, there's more! Arrays work like lockers - each "slot" in an array has an index. The first one in an array is always at index 0, the second one at index 1 and so on. To populate the five-item array, we do this: int[] balls = new int[5]; balls[0] = 42; balls[1] = 43; balls[2] = 44; balls[3] = 45; balls[4] = 46; We can retrieve them just as well: int thirdBall = balls[2]; // Now thirdBall is 44 Why use arrays, then, if we can just declare as many variables as we need? Well, it makes life a lot easier. Say you want a hundred numbers and you want them to run from 1 to 100. You could do it like this: int num1 = 1; int num2 = 2; ... int num100 = 100; Or, you could do this: int[] nums = new int[100]; for(int i = 0; i < 100; ++i) nums[i] = i + 1; Now, after all this: what specifically you have trouble understanding? More on reddit.com
🌐 r/learnjava
11
10
March 29, 2017
🌐
Croma Campus
cromacampus.com › blogs › what-is-an-array-in-java
What is an Array in Java? Step-by-Step Guide
November 7, 2024 - An array in Java is a data structure that stores multiple values of the same type in a single variable, allowing efficient data storage and access by index.
Top answer
1 of 16
3430
Arrays.asList(yourArray).contains(yourValue)

Warning: this doesn't work for arrays of primitives (see the comments).


Since java-8 you can now use Streams.

String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);

To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.

Example

int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
2 of 16
465

Concise update for Java SE 9

Reference arrays are bad. For this case we are after a set. Since Java SE 9 we have Set.of.

private static final Set<String> VALUES = Set.of(
    "AB","BC","CD","AE"
);

"Given String s, is there a good way of testing whether VALUES contains s?"

VALUES.contains(s)

O(1).

The right type, immutable, O(1) and concise. Beautiful.*

Original answer details

Just to clear the code up to start with. We have (corrected):

public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};

This is a mutable static which FindBugs will tell you is very naughty. Do not modify statics and do not allow other code to do so also. At an absolute minimum, the field should be private:

private static final String[] VALUES = new String[] {"AB","BC","CD","AE"};

(Note, you can actually drop the new String[]; bit.)

Reference arrays are still bad and we want a set:

private static final Set<String> VALUES = new HashSet<String>(Arrays.asList(
     new String[] {"AB","BC","CD","AE"}
));

(Paranoid people, such as myself, may feel more at ease if this was wrapped in Collections.unmodifiableSet - it could then even be made public.)

(*To be a little more on brand, the collections API is predictably still missing immutable collection types and the syntax is still far too verbose, for my tastes.)

🌐
Simplilearn
simplilearn.com › home › resources › software development › java tutorial for beginners › arrays in java: declare, define, and access array
Arrays in Java: Declare, Define, and Access Array [Updated]
January 26, 2025 - The article gives a clear insight into the basics of arrays in java, how to define and declare an array in java, type of arrays with an example. So, click here to learn more
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Find elsewhere
🌐
CodeChef
codechef.com › blogs › arrays-in-java
Arrays in Java (With Examples and Practice)
August 7, 2024 - Learn about Arrays, the most common data structure in Java. Understand how to write code using examples and practice problems.
🌐
Baeldung
baeldung.com › home › java › java array › arrays in java: a reference guide
Arrays in Java: A Reference Guide | Baeldung
July 24, 2024 - According to the Java documentation, an array is an object containing a fixed number of values of the same type. The elements of an array are indexed, which means we can access them with numbers (called indices).
🌐
W3Schools
w3schools.com › java › java_arrays_loop.asp
Java Loop Through an Array
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.
🌐
YouTube
youtube.com › watch
Arrays in Java- A complete overview || video 04 || @PurpleCode404 - YouTube
Full Playlist: https://www.youtube.com/playlist?list=PLvHLqo8ycxFEVgsUdrwQJksK2V8mDoTthIn this tutorial, Purple Code introduces the fundamentals of Java arra
Published   May 9, 2023
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Java Examples Java Videos Java ... Q&A Java Certificate ... Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value....
🌐
Princeton CS
introcs.cs.princeton.edu › java › 14array
Arrays
April 8, 2020 - We refer to an array element by putting its index in square brackets after the array name: the code a[i] refers to element i of array a[]. For example, the following code makes an array of n numbers of type double, all initialized to 0: ArrayExamples.java contains typical examples of using arrays in Java.
🌐
CodeChef
codechef.com › practice › arrays
Practice Arrays
Solve Arrays coding problems to start learning data structures and algorithms. This curated set of 23 standard Arrays questions will give you the confidence to solve interview questions.
🌐
Oracle
docs.oracle.com › javase › specs › jls › se7 › html › jls-10.html
Chapter 10. Arrays
3 weeks ago - In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2).
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit7-Arrays › topic-7-1-array-basics.html
7.1. Array Creation and Access — CS Java
An array is a block of memory that stores a collection of data items (elements) of the same type under one name. Arrays are useful whenever you have many elements of data of the same type that you want to keep track of, but you don’t need to name each one. Instead you use the array name and ...
🌐
Programiz
programiz.com › java-programming › multidimensional-array
Java Multidimensional Array (2d and 3d Array)
Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum of 12 elements, ... Remember, Java uses zero-based indexing, that is, indexing of arrays in Java starts with 0 and not 1.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
An array is a collection of elements of the same data type stored in contiguous memory locations. It allows multiple values to be stored under a single name and accessed using an index.
Published   4 weeks ago
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › ArrayBasics › abasics.html
8.1. Arrays in Java — AP CSA Java Review - Obsolete
An array is consecutive storage for multiple items of the same type. You can store a value in an array using an index (location in the array). You can get a value from an array using an index. An array is like a row of lockers, except that you can’t cram lots of stuff into it.