The code of the HappyCoders.eu articles on arrays in Java – one package per article. Every class with a main() method is one chapter of its article and prints the outputs shown there.
| Article | Package |
|---|---|
| How to Initialize Arrays in Java (English, German) | eu.happycoders.initialize |
| Array Length in Java (English, German) | eu.happycoders.length |
| 2D Arrays in Java (English, German) | eu.happycoders.twodimensional |
The examples use Java 25 language features and need Java 25 or newer. Build once with mvn compile, then run a class from the repository root:
java -cp target/classes eu.happycoders.<package>.<ClassName>
Ch1Declare– declaring arrays, initializing them with values (withnew, withvar, withoutnew, C-style), separately from the declaration, with a length, and the default values of all element typesCh2Fill– filling an array element by element, withArrays.fill()(whole array and ranges),Arrays.setAll(), andArrays.parallelSetAll()Ch3Copy–clone()for primitive and object arrays,System.arraycopy(),Arrays.copyOf()(same length, shorter, longer), andArrays.copyOfRange()Ch4Convert– arrays from aListand aSetwith the fourtoArray()variants, from an object stream, and from primitive streams
The outputs on JDK 27 are in results/initialize/outputs-jdk27.txt.
Ch1GetLength–length,Array.getLength(), height and width of a 2D array, andIntSummaryStatisticsof the row lengths, printed with an English and a German default localeCh2SetLength– the length is set when the array is created, with values or with a sizeCh3MemoryLayout– the layout and size of the article's arrays measured with JOL (Java Object Layout):int[5],short[5],byte[5],long[3],long[5],String[4], and the footprint ofint[2][3]Ch4MaxArraySize–byteandlongarrays withInteger.MAX_VALUE,Integer.MAX_VALUE - 1, andInteger.MAX_VALUE - 2elements; needs a heap of 20 GB
JOL needs the JAR with its dependencies, and without the following options it prints warnings about attaching to the JVM:
mvn package
java -Djdk.attach.allowAttachSelf -XX:+EnableDynamicAgentLoading \
-Djol.skipHotspotSAAttach=true --sun-misc-unsafe-memory-access=allow \
-cp target/benchmarks.jar eu.happycoders.length.Ch3MemoryLayout
Add -XX:-UseCompactObjectHeaders for the layout up to Java 26. The maximum array size:
java -Xmx20g -cp target/classes eu.happycoders.length.Ch4MaxArraySize
The outputs on JDK 27 are in results/length/outputs-jdk27.txt.
Ch1Structure– a 2D array is an array of arrays: each row is anint[]of its ownCh2Declare– the brackets at the type, C-style at the name, or mixedCh3Initialize– with values, with default values (new int[2][3]), and row by row (new int[2][])Ch4Access– reading and writing elements, a row as an array, and theArrayIndexOutOfBoundsExceptionfor a wrong row or column indexCh5Length– the number of rows and columns, the empty array, and a row 0 that isnullCh6Iterate– nestedforloops, for-each loops, and a stream withflatMapToInt()Ch7Print–println(),Arrays.toString()andArrays.deepToString(), one line per row, and aligned columns withprintf()Ch8Fill–Arrays.setAll()andArrays.fill()per row, and the pitfall ofArrays.fill()with one row object for all rowsCh9Jagged– rows of different lengths, and Pascal's triangle as a jagged arrayCh10Copy–clone()copies only the outer array; a deep copy with a loop and with a stream;Arrays.equals()vs.Arrays.deepEquals()Ch11Sort– every row for itself, and the rows by one of their columns with aComparatorCh12Multidimensional– a 3D array, andArray.newInstance()for dimensions known only at runtimeCh13Flat– a 3 x 4 matrix in a flat 1D array, element[row][col]at indexrow * columns + col
The outputs of all chapters on JDK 27 are in results/twodimensional/outputs-jdk27.txt.
Each article's JMH benchmarks are in the benchmark subpackage of its package:
TraversalBenchmark– sums all elements of a squareintmatrix with 64, 1,024 and 4,096 rows and columns, stored as a 2D array and as a flat 1D array, each in row-major order (the inner loop walks along a row) and in column-major order (the inner loop walks down a column)
mvn package builds target/benchmarks.jar with all of them. Run the benchmarks of one article with its package as filter:
mvn package
java -jar target/benchmarks.jar eu.happycoders.twodimensional.benchmark
The traversal benchmark takes about four minutes. The results of the articles' runs are in results/<package>/, e.g. results/twodimensional/apple-m5-pro/. The file traversal-jdk27-size-4100.log there is a check run with 4,100 instead of 4,096 columns: at 4,096 columns the elements of one column of the flat array are exactly 16 KB apart and evict each other from the same cache sets, which makes the column-major flat traversal slower than the 2D one; at 4,100 columns it is faster.
Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!
- Avoid lengthy research with this concise overview of all Java versions from Java 10 to Java 27.
- Discover the innovative features of each new Java version, summarized on a single page.
- Impress your team with your up-to-date knowledge of the latest Java version.
👉 Download the free Java Versions PDF
(Hier geht's zur deutschen Version → Java-Versionen PDF)
With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.
- Always choose the most efficient data structures and thus increase the performance of your applications.
- Be prepared for technical interviews and confidently present your algorithm knowledge.
- Become a sought-after problem solver and be known for systematically tackling complex problems.
👉 Download the free Big O Cheat Sheet
(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)
👉 Want to stay on top of modern Java? Sign up for the HappyCoders newsletter – Modern Java: new versions & features, performance, and JVM insights – once a month.
(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)
🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen
Trage dich jetzt unverbindlich auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!


