Arrays and Collections in Java: A Complete Guide for Beginners — Part 1

онлайн тренажер по питону

Arrays and Collections in Java: A Complete Guide for Beginners — Part 1

Learning Java is impossible without understanding how to store and manage groups of data. In this article, we will cover two fundamental topics: arrays and collections. This is the first part of a complete guide, suitable for learning from scratch and will help systematize your knowledge of programming in Java.



What are arrays in Java?

An array is a fixed-length data structure that stores elements of the same type. Arrays are the foundation of Java for beginners because they are simple and fast. However, their main drawback is that the size cannot be changed after creation.



Declaring and initializing an array

In Java, an array is declared using square brackets. Here is the basic syntax:

int[] numbers = new int[5]; // array of 5 integersString[] names = {"Anna", "Boris", "Viktor"}; // literal initialization


Accessing elements and array length

Indexing starts at 0. The array length is stored in the length property:

int first = numbers[0];int size = numbers.length; // 5


Loops for working with arrays

It is convenient to use for or for-each to iterate over elements:

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

for (int num : numbers) { System.out.println(num);}


Multidimensional arrays

Java supports arrays of arrays. For example, a two-dimensional array is a table:

int[][] matrix = new int[3][4]; // 3 rows, 4 columnsmatrix[1][2] = 42;

Multidimensional arrays are useful for matrices, game boards, and images.



Collections in Java: a flexible alternative

Collections are dynamic data structures from the Java Collections Framework (JCF). They solve the main problem of arrays: they automatically expand and shrink. For programming in Java, collections are a must-have.



Main collection interfaces

  • List — an ordered list with duplicates (ArrayList, LinkedList).
  • Set — a set without duplicates (HashSet, TreeSet).
  • Map — a dictionary: key → value (HashMap, TreeMap).


ArrayList: dynamic array

The most popular type of collection. Usage example:

import java.util.ArrayList;

ArrayList<String> list = new ArrayList<>();list.add("Java");list.add("Python");list.add("C++");System.out.println(list.get(0)); // JavaSystem.out.println(list.size()); // 3


HashSet: unique elements

Use Set when you need to exclude duplicates:

import java.util.HashSet;

HashSet<Integer> set = new HashSet<>();set.add(10);set.add(20);set.add(10); // will not be addedSystem.out.println(set.size()); // 2


HashMap: key-value pairs

Great for fast searching:

import java.util.HashMap;

HashMap<String, Integer> map = new HashMap<>();map.put("Alice", 25);map.put("Bob", 30);System.out.println(map.get("Alice")); // 25


When to use arrays and when to use collections?

The choice depends on the task:

  • Arrays — if the size is fixed, maximum performance is needed, or you are working with primitives.
  • Collections — if the data changes dynamically, or you need uniqueness, sorting, or key-based search.

For learning Java, it is recommended to start with arrays and then move on to collections.



Example: combining arrays and collections

Consider a task: convert an array to a list and back

Похожие статьи

Книги по Python