In this Java article we want to learn about Arrays and Collections in Java, arrays and collections are fundamental concepts in Java programming. in this article we are going to talk that what arrays and collections are, how they works and what are differences between them.
Arrays in Java
In Java an array is data structure that stores fixed size sequential collection of elements of the same type. it is a container object that holds fixed number of values of single type. each element in the array can be accessed by an index that represents its position in the array.
For creating an array in Java, you need to specify data type of array elements and size of the array. for example to create an array of integers with 5 elements, you can use this code, so in this code int[] keyword specifies that we are creating an array of integers, and new int[5] statement creates an array with 5 elements.
1 |
int[] numbers = new int[5]; |
For accessing an element in the array, you need to use its index, which starts from 0. for example, to access second element in the array, you can use this code, this code retrieves the value of first element in the array and stores it in the variable firstNumber.
1 |
int firstNumber = numbers[0]; |
Arrays are useful for storing and manipulating fixed number of elements of the same type. but they have some limitations. for example, once an array is created, its size cannot be changed. also arrays can only store values of the same data type.
Collections in Java
Collections in Java are similar to arrays in that they store a collection of elements. but collections are more flexible than arrays, as they can store elements of different types and can be resized dynamically.
In Java collections are implemented using Collection interface. some of the most commonly used collection interfaces in Java are:
List | An ordered collection of elements that allows duplicates |
Set | An unordered collection of unique elements |
Map | A collection of key-value pairs |
To use collections in Java, you need to import appropriate classes from java.util package. for example, to create a list of strings, you need to use the like this, in this code List<String> keyword specifies that we are creating a list of strings, and ArrayList<String>() statement creates an empty list.
1 2 3 4 |
import java.util.List; import java.util.ArrayList; List<String> names = new ArrayList<String>(); |
For adding an element to the list, you can use add() method like this.
1 |
names.add("Geekscoders.com"); |
To retrieve an element from the list, you can use get() method which takes an index as an argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.List; import java.util.ArrayList; public class HelloWorld { public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Geekscoders.com"); String firstName = names.get(0); System.out.println(firstName); } } |
This code retrieves the value of first element in the list and stores it in the variable firstName.
So Collections are more flexible than arrays, as they can store elements of different types and can be resized dynamically. also collections provides different methods for manipulating the elements they contain.