0% found this document useful (0 votes)
186 views21 pages

CSC 201-Lecture 7

This document discusses arrays in Java, including: 1) Arrays allow storing multiple values of the same type in a single variable through indexes. For example, an array can store exam marks for 1000 students. 2) Arrays have a fixed size set at creation through the new operator. Individual elements are accessed via indexes starting from 0. 3) Common array operations include initialization, accessing elements, looping, and calculating sums or averages of elements. Two-dimensional arrays represent matrices.

Uploaded by

pavanil
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views21 pages

CSC 201-Lecture 7

This document discusses arrays in Java, including: 1) Arrays allow storing multiple values of the same type in a single variable through indexes. For example, an array can store exam marks for 1000 students. 2) Arrays have a fixed size set at creation through the new operator. Individual elements are accessed via indexes starting from 0. 3) Common array operations include initialization, accessing elements, looping, and calculating sums or averages of elements. Two-dimensional arrays represent matrices.

Uploaded by

pavanil
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

CSC 201

Lecture - 7
Scenario
• Let us take a University scenario. I want to
calculate the grade of all the students
based on their marks in each course. How
do I do?
Arrays in Java
• An array is a group of variables of the same data
type referred to by a common name
• It is a contiguous block of memory locations
referred by a common name.

Ex : To store the marks of 1000 students, I can


declare an array namely student_marks of size
1000 and store them.
How does an Array look?

Student 0
Student 1

Student 2
Student 3

Each item in an array is


called Element. The
numbering begins with ‘0’.
Declaring an Array
We can declare an array as below:-
1)datatype variable[] = new datatype[size];
Ex: int students[] = new int[20];
or
datatype[] variable = new datatype[size];
2) datatype[] variable;
variable = new datatype[size];
Ex: int[] students;
students = new int[20];
Creating Arrays
• The length of an array is fixed at the time
of creation.
• A specific value in an array is accessed by
placing the index value of desired element
in square bracket.
Ex : If I want to access the 5th student in
an array, I would say student[4].
Structure of Arrays
• Array contains the values which are
referenced through indexes.
• To access the stored values in an array,
we use indexes. Suppose an array
contains ‘n’ integers, the first element can
be indexed with a ‘0’ value and last
element by ‘n-1’ index value.
Advantages of using Arrays
1) We can refer to a large number of elements by
just specifying the index number and the array
name.
2) Arrays make it easy to do calculations in a
loop.
3) An array has its size that is known as array
length.
Various types of arrays in Java:-
5) One – dimensional
6) Two – dimensional
Disadvantages of arrays
1) An array has fixed size.
2) Array holds only one type of data
(including the primitive types).
Memory Allocation
• New operator is used to allocate memory
to an array.
Ex: student_name = new String[10];

• Where else did we use a new operator


previously?
Declaring simple arrays
Public static void main(String[] args)
{
int[] a = new int[5];
int[] b = {1,2,3,4,5}; Initializing an array!

String[] name = { “student1”, “student2”};


char[] c = {‘a’, ‘b’};
}
Simple program to print array
elements
public static void main(String[] args)
{
int [] a = {1,2,3,4,5};
for(int i =0; i < 5; i++)
System.out.println(“Elements of array a
are “+a[i]);}
Sample Program
• What happens in the following scenario?

public static void main(String[] args)


{
int [] a= {1,2,3,4,5}; Array Index out of bounds?

for(int i =0; i < 6; i++)


System.out.println(“Elements of array a are
“+a[i]);
}
public static void main(String[] args)
{
int [] a= {1,2,3,4,5};
for(int i =0; i < 5; i++)
{ System.out.println(“Elements of array a are
“+a[i]);}

System.out.println(a[2]); What gets printed


here?
}
Program to calculate sum of
numbers from 1 to 10
Public static void main(String[] args)
{
int a[] = new int[10];
int sum = 0;
for(int k =0; k < 10; k++) Do you get the sum of first
{ 10 numbers?

a[k] = k;
sum = sum + a[k];
}
System.out.println(“Sum of first 10 numbers is “+sum);
}// How about k < 11? How about increasing size of array?
Calculate the sum
public class sum
{
public static void main(String[] args)
{
int[] a = new int[11];
int sum = 0;
for(int m=0; m < a.length ; m++)
a[m] = m;
for(int k =0; k< a.length; k++)
sum += a[k];
System.out.println(sum);
}
}
Output here ?
Example- Printing 10 random
numbers
import java.util.Random;
public static void main(String[] args)
{
Random mynumber = new Random();
int[] student_marks = new int[10];
for(int i = 0; i < 10 ; i++)
{
student_marks[i] = mynumber.nextInt(10);
System.out.println(“Marks for student “+i” is
“student_marks[i]);
}
}
• What if you want to get an average of the
marks of all the students in a class?
Program to search a number in an
Array
Public static void main(String[] args)
{
int a[] = {10,5,3,12,45,7}; int i=0, found=0;
System.out.println(“Enter a number to search in the array:”);
Scanner s = new Scanner(System.in);
int num = s.nextInt();
while( i < a.length)
{
if(num == a[i])
{System.out.println(“Yes, it matched!”); found = 1;
break;
}
i++;
}
If(found == 0)
System.out.println(“Sorry, not found”);
}
What if you want the position of the element in the array if the number is found?
Two dimensional Arrays
• 2-D arrays are defined as an Array of
arrays.
• An array of ints will have type int[],
similarly we can have int[][], which means
array of arrays of ints.
Print a matrix using a 2–D array
Public static void main(String[] args)
{
int a[][] = new int[2][2];
for(int i=0; i< a.length;i++)
{
for(int j=0;j < a.length; j++)
{
a[i][j] = i;
System.out.print(a[i][j]);
}
System.out.println(“ “);
}
}

You might also like