# Arrays

of 6 Arrays

## Content

ARRAYS
Suppose a class contain 50 student, and we want to store their roll numbers, we need 50 separate
variables for storing the roll numbers, as shown here:
intrno;
int rno1;
int rno2;
.
.
.
.
int rno49;
Now to store roll numbers into these variables, we need to add another 50 students.Imagine writing
100 statements just to store the roll numbers of students.
On the other hand, if we have a single variable which can represents all these 50 variables,it would be
very useful to us. Such variable is called an 'array'.
An Array represents a group of elements of same data type. It can store a group of elements. So we
can store a group of int values or a group of float values or a group of strings in the array.
Note: We cannot store some integer values and some float values in the array.
syntax to create an Array:
int rollNo[]={001,002,003,004,005,006};
float sal[]={2500.00f,30000.00f,35000.00f}
char ch[]={'a','b','c','z'};
double d[]={121.12,123.34,1234.4322};
Alternate way of writing one dimensional Array
int[] rollNo={001,002,003,004,005,006};
float[] sal={2500.00f,30000.00f,35000.00f}
char[] ch={'a','b','c','z'};
double[] d={121.12,123.34,1234.4322};
Types of Arrays
Arrays are generally categorized into two parts as described here:
Single dimensional arrays(or ID arrays)
Multi dimensionalarrays(or 2D,3D,…..arrays)
Single dimensional Array(1D array):
A one dimensional or single dimensional array represents a row or a column or a column of elements.
What I explained above come under single dimensional array.

Another way of creating one dimensional array is by declaring the array first and then allotting
memory for it by using new operator

int marks[];
marks= new int;
These two statements can be combined into single statement
int marks[]= new int;
Here we should understand that JVM allots memory for storing 5 integer elements into the array.
But there are no actual elements stored so far. To store the elements into the array, we can use
statements like these
marks=50;
marks=60;
marks=70;
marks=80;
small example:
for(int i=0;i<=5;i++){
,marks[i]=i
}
Small example:
public class Arry {
public static voidmain(String[] args) {
int marks[]=newint;
for(int i=0;i<5;i++){
marks[i]=i;
System.out.println(marks[i]);
}
}
Write a program to create 1D array and read its elements by using a loop and display them one by
one.
To read and display one by one.
public class Arr1 {
public static void main(String[] args) {
int arr[]={10,60,70,80};
for(int i=0; i<arr.length; i++){
System.out.println(arr[i]);
}}}
Write a program which accepts the marks of student into a 1D array from the keyboard and finds total
marks and percentage.
import java.io.*;
importjava.lang.*;
class Arry1
{
Public static void main(String args[])throws IOException
{
int marks[]={50,60,55,50,67};
int tot=0;
for(int i=0;i<marks.length;i++)
{
System.out.println(marks[i]);
tot=tot+marks[i];

}
System.out.println("total marks="+tot);
float percentage=(float)tot/marks.length;
System.out.println("percentage="+percentage);
}
}
Home work
Write a program which performs sorting of group of integer values using bubble sort technique.
(Sort a group of integers into ascending order)
public class Arr17{
public static void main(String[] args) {
int arr[]={50,23,11,99,23};
int limit=arr.length-1;
boolean flag= false;
int temp;
for(int i=0;i<limit; i++){
for(int j=0;j<limit-i;j++){
if(arr[j]>arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
flag=true;
}}
if(flag == false) break;
else flag=false;
}
System.out.println("The sorted array is:");
for(int i=0;i<arr.length; i++){
System.out.println(arr[i]);
}}}
Command Line Arguments:
Command line arguments means the values passed to main() method. To catch
and store these values, main() has a parameter, String args[] as:
public static void main(String args[])
Here args[] is one dimensional array of String type. So it can store a
group of strings, passed to main() form outside by the user. The user
should pass the values from outside ,at the time of running the program at
command prompt
>javaprog 11 22 vikas
Small example on command line argument
Public class ArrCommand {
Public static voidmain(String[] args) {
int n=args.length;
System.out.println("no foargs="+n);
System.out.println("write these the args are:");
for(int i=0;i<n;i++){
System.out.println(args[i]);
}}}

Javac prog.java
Java prog 11 12 vikas
No of args=3
The args are:
11
22
Vikas
Multi DimensionalArray(2D,3D,….arrays)
Two dimensional array(2D array)
A two dimensional array represents several rows and several columns
Of data. For example,the marks obtained by a group of students in five
different subjects can be represented by a 2D array.
Declare two dimensional array as:
int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}}
Or
int[] marks={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}}
It will store in matrix order I will explain you very clear.
Write a program take the 2d array and display in matrix form.
importjava.io.*;
importjava.lang.*;
class Array19
{
publicstaticvoid main(String args[])
{
int a[][]={{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};
System.out.println("in matrix form");
for(int i=0;i<3;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}
}
}

We can consider a three array as a combination of several two dimensional
arrays. This concept is useful when we want to handle group of elements
belonging to another group. for example ,a college has 3 departments:
electronics, computers and civil. We want to represent the marks obtained by the
students of each students of each department in 3 different subjects. We can
write these marks as shown here:

Electronics department:
Student 1 marks: 50,51,52
Students 2 marks: 60, 61, 62
Computers departments
Student 1 marks: 70,71,72
Students 2 marks: 80, 81,82
Civil departments
Student 1 marks:60,71,72
Students 2 marks: 80, 81,82
int arr[][][]={{{50,51,52},{60,61,62}},{{70,71,72},{80,81,82}},{{65,66,67},
{75,76,77}}};
Here, three pairs of square braces represent a 3D array and there are 3 rows
representing the 3 departments. In each department, there are again two
groups representing the marks of two students. Observe how to {and} curly
braces are used to represent each group. Other ways of creating three
dimensional arrays.
char x[][][]=new char;
float [][][]f=new float;
Write a program in which we take 3D array which consists of department wise
student marks in 3 subjects. We want to calculate total marks of each student.

package org.sample;
public class ThreeDimensional {
public static void main(String[] args) {
int dept,student,maarks,tot=0;
int arr[][][]={{{50,51,52},{60,61,62}},
{{70,71,72},{80,81,82}},
{{65,66,67},{75,76,77}}};
for (dept = 0; dept < 3; dept++) {

System.out.println("department "+(dept+1)+":
");
for ( student = 0; student < 2; student++) {
for (int marks=0; marks< 3; marks++) {
System.out.println(arr[dept][student][marks]+"
");
tot+=arr[dept][student][marks];
}
System.out.println("Total: "+tot);
tot=0;
}
}
}
}

## Recommended

Or use your account on DocShare.tips

Hide