GamifiedGamified challengesPracticeRaceLeaderboardPricing

Learn data structures by watching them move, then doing it yourself. Press M anytime to toggle sound.

A product of Amypo Technologies, Coimbatore.

Learn

  • Fundamentals & OOPs
  • Data structures and algorithms
  • Aptitude
  • Learning path
  • How it works
  • About dotprep

Practice

  • Practice arena
  • Daily challenge
  • Gamified challenges
  • Race mode
  • Leaderboard

Interview prep

  • Java interview
  • Coding questions
  • Company questions
  • System design
  • Jobs & careers

You

  • Dashboard
  • Settings
  • Pricing
  • Create account

© 2026 dotprep

Content last updated 27 November 2026

GamifiedGamified challengesPracticeRaceLeaderboardPricing
Log inStart free

Free preview: 3 of 3 free topics used. You can come back to these anytime; other topics need a free account.

Sign up free to unlock all 113
Fundamentals & OOPsProgramming fundamentals

Lists: many values, one name

beginnerone name for a row of numbered slots; the numbering starts at 0
Before this: Variables, loops and functions: a list is many variables in a row, and a loop walks along it.

1. The problem

A teacher has the marks of 60 students and wants to know the highest. With plain variables she needs marks1, marks2 and so on up to marks60: sixty names to type. To find the highest she must write a comparison for every one of them. If one more student joins, the program has to be written again. These values are all the same kind of thing, so they belong together under one name.

How can a program keep many values under one name and still reach each one of them?

2. Picture it first

A list is a row of lockers with one name on the whole row. Each locker holds one value and has a number on its door. The numbers start at 0, not 1. To use a locker you say the name of the row and the number on the door.

Every part of the picture has a name in code:

In the pictureIn code
A row of lockers with one namea list (also called an array): marks
One lockera slot (also called an element)
The number on a locker doorthe index: 0, 1, 2 and so on
Opening door number 2 to look insidemarks[2]
Putting a new paper in the last lockermarks[3] = 96
How many lockers the row hasthe length of the list
Walking along the row, opening each doora loop over the indexes

3. Watch the story, built from zero

Story mode

Story mode

20 scenes · about 4 min · narrated
  • just created
  • slot being read or changed
  • already looked at

In real life

🗄️[0]62
🗄️[1]85
🗄️[2]47
🗄️[3]91

A row of lockers with one student's marks in each. The doors are numbered from 0.

In the computer's memory

Variables

—

On the screen

Nothing printed yet

1public class Main {
2 public static void main(String[] args) {
3 int[] marks = {62, 85, 47, 91}; // a list of marks
4 System.out.println(marks[2]); // read one slot
5 marks[3] = 96; // change one slot
6 int best = marks[0]; // highest so far
7 for (int i = 1; i < marks.length; i++) { // i = 1 to 3
8 if (marks[i] > best) {
9 best = marks[i];
10 }
11 }
12 System.out.println(best);
13 }
14}

1 / 20
EnglishThis lesson is in English for now

4. Separate variables vs Lists: many values, one name

Separate variablesLists: many values, one name
Names you must inventOne for every value: m1, m2, m3One for all of them: marks
Reaching one valueBy its own nameBy its index: marks[2]
Doing the same thing to allOne line for every variableOne loop
One more value arrivesWrite more codeSame code, longer list
Good forA few different thingsMany things of the same kind

5. Common mistakes

  • Counting from 1: writing marks[1] to get the first mark.

    The first slot is marks[0]. marks[1] is the second slot.

  • Using the number of slots as the last index: marks[4] in a list of 4.

    The last index is one less than the number of slots: marks[3]. There is no slot after it, so the program stops with an error or reads a rubbish value.

  • Mixing up the index and the value, and thinking marks[2] is 2.

    The index says where to look. The value is what is stored there. marks[2] is whatever the third slot holds, such as 47.

  • Letting the loop run one round too many by going on while i is equal to the length.

    Go on only while i is less than the length. The indexes run from 0 to the length minus 1.

6. Where you'll meet it

A class register

The marks of every student are kept in one list. A loop finds the topper, the total or the average.

A shop bill

The prices of the things you bought are a list. Adding them up in a loop gives the amount to pay in ₹.

Words are lists of letters

A piece of text is a row of letters, and each letter has an index that starts at 0.

See that lesson

Arrays in depth

How a list sits in the computer's memory, and what it costs to add or remove a value in the middle.

See that lesson

7. Check yourself

Answer these before you watch it move. Each answer explains itself.

Question 1

1.marks = [62, 85, 47, 91]. What is marks[1]?

Question 2

2.A list has 6 slots. What is the index of the last slot?

Question 3

3.marks = [10, 20, 30]. The line marks[0] = 50 runs. What is the list now?

Question 4

4.Why is a list better than 60 separate variables for 60 marks?

In short

  1. 1A list keeps many values under one name. Each value sits in its own slot. Some languages call a list an array.
  2. 2Every slot has a number called its index. The first slot has index 0, the next has index 1, and so on.
  3. 3marks[2] means the slot of marks with index 2, which is the third slot. You can read it, or store a new value in it.
  4. 4Storing a new value in one slot leaves every other slot as it was.
  5. 5The last index is the number of slots minus 1. There is no slot after it, and asking for one is a mistake.
  6. 6A loop that counts through the indexes visits every slot, so the same few lines work for a list of any length.

On this page

  • The problem
  • Picture it first
  • Story mode
  • Compare
  • Common mistakes
  • Where you'll meet it
  • Check yourself
  • In short

Key facts

Create
marks = [62, 85, 47, 91]
First index
0
Last index
number of slots − 1
Read a slot
marks[2]
Change a slot
marks[2] = 50

When to use it

Whenever a program has many values of the same kind: the marks of a class, the prices on a bill, the temperature on each day of a month. One name and one loop replace dozens of separate variables.