CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 7 min · free

One-dimensional Arrays

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

What an array is, declaring, creating and initialising it, default values, traversal with length, and the sum, average, maximum and minimum programs.

Follows the syllabus of: CBSE Class 12 Information Technology (802), ICSE Class 10 Computer Applications, ISC Class 11 Computer Science (868), ISC Class 12 Computer Science (868)

On screen in this lesson

What is an array?

A group of values of the same type under one name
The size is fixed when the array is created
Each value sits in a numbered box called an element
Box numbers are indexes, and they start at 0

Numbered boxes: marks

IndexElementValue
0marks[0]72
1marks[1]85
2marks[2]64
3marks[3]90
4marks[4]58

Default values

TypeDefaultExample
int, long0new int[3]
double0.0new double[3]
booleanfalsenew boolean[3]
char'\u0000'new char[3]
Stringnullnew String[3]

Arrays in Java and C

PointJavaC
Createnew int[5]int m[5];
Sizem.lengthno length field
Wrong indexexceptionno check, risky

Quick recap

An array stores same-type values in numbered boxes
Index starts at 0; the last index is length - 1
new creates boxes with default values; {} fills them
Traverse with i < m.length
Sum, average, max and min use one loop each

Quick answers

Why start max at m[0] and not 0?

If every value is negative, 0 would wrongly win.

Why cast the sum to double for the average?

Otherwise whole number division drops the decimal, giving 77 instead of 77.75.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Your class has forty students. Will you make forty variables to store their marks? No! One array can hold them all. Today we learn what an array is, how to create it, fill it, and find the sum, average, maximum and minimum.

An array is like a row of lockers in a school corridor. It stores many values of the same type, like all integers, under one name. Its size is fixed when it is created, so it cannot grow later. Each box in the array is called an element. The number on each box is its index, and the first index is zero.

Picture an array called marks with five boxes. Box zero holds seventy two, and we read it as marks of zero. Box one holds eighty five. Box two holds sixty four. Box three holds ninety. Box four holds fifty eight. It is the last box, because five boxes are numbered zero to four.

Making an array has steps. First we declare it. Int with square brackets means marks will refer to an array of integers. Then new int five creates five boxes in memory. Then we store seventy two in box zero. Now predict. What is in box one? We never stored anything there. Java fills new integer boxes with zero, so it prints zero.

When new creates an array, Java puts a default value in every box. Whole number arrays like int and long start with zero. A double array starts with zero point zero. A boolean array starts with false. A char array starts with the null character, which is character code zero. A String array starts with null, which means no object yet.

If you already know the values, write them inside curly brackets. This declares, creates and fills the array in one line. Here M is short for marks. Java counts the values and makes the size five. M dot length gives the size, so it prints five. M of four is the last box, fifty eight. Careful: M of five does not exist. It throws an Array Index Out Of Bounds Exception.

Traversal means visiting every element one by one. We use a for loop whose counter runs from zero up to length minus one. The condition is, counter less than M dot length. So the loop visits index zero, one and two, and prints each mark. Using length is better than typing three, because it still works if the array size changes. Notice that length for an array has no brackets, but a String uses length with brackets.

Most exam programs read the values from the user. First, import java dot util, which contains the Scanner class. A Scanner object reads what the user types, and here it is named S C. We create an empty array of five boxes. The loop runs five times, and next int reads one whole number into each box. If the user types seventy two, eighty five, sixty four, ninety and fifty eight, the array matches our earlier picture.

Now the four calculations that boards ask most. First, sum and average. S is the sum, and it starts at zero. The loop adds every mark to S, so S becomes three hundred eleven. The average is the sum divided by the number of marks. We write double in brackets before S, so Java keeps the decimal part. Without it, whole number division would give seventy seven instead of seventy seven point seven five.

To find the maximum, we first assume the first mark is the biggest. So max starts as M of zero, which is seventy two. The loop starts from index one and compares each mark with max. Whenever a mark is bigger, max takes that value. Eighty five replaces seventy two, and then ninety replaces eighty five. So it prints ninety. Pause and predict. Why not start max at zero? If every value is negative, zero would wrongly win.

The minimum works the same way, with one change. Min starts as the first mark, seventy two. Now we check whether each mark is smaller, using the less than sign. Sixty four replaces seventy two, and then fifty eight replaces sixty four. So, for the same five marks, this prints fifty eight.

If you also study C, compare the two languages. In Java we create boxes with new int five, while C simply writes int M of five. Java arrays know their size through length, but C arrays have no length field. Java stops a wrong index with an exception, while C does not check, which can give garbage values.

Some exams ask for arrays in pseudocode. Declare marks as an array from one to five of integer. In this pseudocode, the first index is usually one, not zero. The for loop adds each mark to total, and the last line outputs the average.

Let us revise. An array stores many values of the same type in numbered boxes. The index starts at zero, and the last index is length minus one. The word new creates boxes with default values, and curly brackets fill them directly. Traverse with a loop that runs while the counter is less than length. Sum, average, maximum and minimum each need just one loop, so practise all four today.

Courses that teach this

CourseUnit
CBSE Class 12 Information Technology (802)Fundamentals of Java Programming
ICSE Class 10 Computer ApplicationsArrays
ISC Class 11 Computer Science (868)Arrays and Strings
ISC Class 12 Computer Science (868)Arrays and Strings
GSEB Std 12 Computer StudiesClasses, Objects, Arrays and Strings in Java
Cambridge IGCSE Grade 9 Computer Science (0478)8. Programming
Cambridge IGCSE Grade 10 Computer Science (0478)8. Programming
Programming All levels JavaArrays and Strings
Programming All levels CStructures, Unions, and Enums

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →