Methoden von Arrays und Collections in Java: Ein ausführlicher Leitfaden
What is it and why? (a real-life analogy)
Imagine you are a physical education teacher, and you have a list of students in your class. This list is an array or a collection (list) of names. Without methods, you would have to do everything manually: rewrite the list by hand if someone is sick, search for a student's last name with your eyes, count how many people are in the class. This is slow and inconvenient.
Methods of arrays and collections are like a set of magic tools that do all the routine work for you. Want to know if the student "Petrov" is on the list? Just call the method contains("Petrov"). Need to sort everyone alphabetically? Use sort(). Want to add a new student? Write add("Newcomer").
In real life, we constantly work with lists: a shopping list, a playlist on your phone, contacts in an address book. Programs also constantly work with data sets. Methods are ready-made commands that save the programmer time and effort, allowing them not to reinvent the wheel every time they need to do something with a list.
💡 Important: Arrays in Java are basic structures, while collections (e.g., ArrayList) are more advanced and convenient "containers" for storing data. Both have methods, but collections have many more.
Theory: how it works internally
Let's look "under the hood". When you create an array in Java, you allocate a continuous chain of cells in the computer's memory. Each cell stores one value (a number, a string, an object). Each cell has a number — an index. The first cell has index 0, the second has index 1, and so on.
Array methods are built-in functions that can traverse these cells and do something with them. For example, the Arrays.sort() method rearranges the values in the cells so that they are in ascending order. It does not create a new array; it changes the order in the same one.
Collections (e.g., ArrayList) work more cleverly. Internally, they also have an array, but it is "rubber". When you add a new element and there is no space left in the array, the collection automatically creates a new, larger array, copies all the old elements into it, and adds the new one. The old array is deleted. This process is called dynamic expansion.
Collection methods are divided into several groups:
- Adding:
add(),addAll() - Removing:
remove(),clear() - Searching:
contains(),indexOf() - Modifying:
set(),sort() - Information:
size(),isEmpty()
Each method is a small program that performs one specific task. For example, the contains() method goes through all the elements of the collection and compares each one with what you are looking for. If it finds a match, it returns true; if not, it returns false.
⚠️ Remember: Arrays have a fixed length. If you create an array of 10 elements, you cannot add an 11th one. Collections (ArrayList) can grow and shrink automatically.
Syntax in Java: all forms and variants
Let's break down the syntax using two of the most popular structures as examples: an array and an ArrayList.
1. Arrays (class Arrays)
To work with array methods, you need to import java.util.Arrays.
| Method | What it does | Example |
|---|---|---|
sort(array) |
Sorts the array in ascending order | Arrays.sort(arr); |
toString(array) |
Converts the array into a string for output | Arrays.toString(arr) |
binarySearch(array, key) |
Searches for an element in a sorted array | Arrays.binarySearch(arr, 5) |
fill(array, value) |
Fills the entire array with one value | Arrays.fill(arr, 0); |
copyOf(array, length) |
Copies the array (can truncate or expand) | Arrays.copyOf(arr, 10) |
equals(array1, array2) |
Compares two arrays element by element | Arrays.equals(a, b) |
2. Collections (ArrayList)
To work with ArrayList, you need to import java.util.ArrayList and java.util.Collections (for additional methods).
| Method | What it does | Example |
|---|---|---|
add(element) |
Adds an element to the end of the list | list.add("Hello"); |
add(index, element) |
Adds an element at the specified position | list.add(0, "First"); |
get(index) |
Returns the element at the specified index | String s = list.get(2); |