Arrays and ArrayLists
Build lists of data with Java arrays and ArrayLists.
StartKey Concepts
Review core concepts you need to learn to master this subject
Index
Arrays
Array creation in Java
Changing an Element Value
Java ArrayList
Modifying ArrayLists in Java
Index
Index
int[] marks = {50, 55, 60, 70, 80};
System.out.println(marks[0]);
// Output: 50
System.out.println(marks[4]);
// Output: 80
An index refers to an element’s position within an array.
The index of an array starts from 0 and goes up to one less than the total length of the array.
Learn Java: Arrays
Lesson 1 of 2
- 1We have seen how to store single pieces of data in variables. What happens when we need to store a group of data? What if we have a list of students in a classroom? Or a ranking of the top 10 horse…
- 2Imagine that we’re using a program to keep track of the prices of different clothing items we want to buy. We would want a list of the prices and a list of the items they correspond to. To create a…
- 3When we printed out the array we created in the last exercise, we saw a memory address that did not help us understand what was contained in the array. If we want to have a more descriptive printo…
- 4Now that we have an array declared and initialized, we want to be able to get values out of it. We use square brackets, [ and ], to access data at a certain index: double[] prices = {13.1, 15.87,…
- 5We can also create empty arrays and then fill the items one by one. Empty arrays have to be initialized with a fixed size: String[] menuItems = new String[5]; Once you declare this size, it cann…
- 7When we write main() methods for our programs, we use the parameter String[] args. Now that we know about array syntax, we can start to parse what this means. A String[] is an array made up of St…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory