Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Array and String

Uploaded by

prachichavda0611
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Array and String

Uploaded by

prachichavda0611
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Chap-9 WORKING WITH ARRAY

NOTES AND PRACTICAL PREPARE BY


SONAL MAM
😊 MY DEAR STUDENTS I HOPE THESE NOTES
HELP YOU
ARRAY: -
ARRAY WHICH STORES A FIXED-SIZE SEQUENTIAL
COLLECTION OF ELEMENTS
OF THE SAME TYPE.
An ARRAY IS USED TO STORE A COLLECTION
OF DATA SAME TYPE.
DECLARING ARRAY VARIABLES
SYNTAX: -
Data type[]arrayRefvar;
EXAMPLE: -
THE FOLLOWING CODE SNIPPETS ARE EXAMPLE
OF THIS SYNTAX: -
double[]mylist;
CREATING ARRAYS
You can create an array by using the new operator
with the following syntax:-
Syntax:-
arrayRefvar=new datatype[array size];
The above statement does two things:-
 It creates an array using new
datatype[arraysize]
 It assigns the reference of the newly created
array to the variable arrayRefvar.
Note:-Declaring +creating +assigning an array
combined in one statement.
datatype[] arrayRefvar=new datatype[array size];
OR
datatype[]arrayRefvar={value0,value1…..};
Example:-
double[] mylist=new double[10];

PROCESSING ARRAYS
(coading1 for practical)

public class Testarray


{
public static void main(String[]args)
{
double[] mylist={1.9,2.9,3.4,3.5};
//print all the array elements
for(int i=0;i<mylist.length;i++)
{
System.out.println(mylist[i]+’’ ‘’);
}
//sum of all elements
double total=0;
for(int i=0;i<mylist.length;i++)
{
total+ =mylist[i];
}
System.out.println(“Total is “ + total);
//finding the largest elements
double max=mylist[0];
for(int i=1;i<mylist.length;I++)
{
If(mylist[i]>max)max=mylist[i];
}
System.out.println(“Max is” +max);
}
}

output of this coading


1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5

(coading2 for practical)


class ArrayAvg

{
public static void main(String[]s)
{
double num[]={10.5,20.6,30.8,15.5,17.3,25.5,27.2};
byte ctr;
double sum=0;
double avg;
System.out.println("list of number is");
for(ctr=0;ctr<10;ctr++)
{
System.out.println(num[ctr]);
sum=sum+num[ctr];
}
avg=sum/10;
System.out.println("\n Average of above
numbers is"+avg);
}
}
(coading3 for practical)

class Array2D
{
public static void main(String[] s)
{
int marks1[][]= new int[5][3];
int marks2[][]=new int[5][3];
int marks3[][]=new int[5][3];
int marks4[][]={{50,60,70},{35,30,50},
{70,75,80},{80,85,90},
{30,50,55}};
int marks5[][]={{50,60,70},{35,30,50},
{70,75,80},{80,85,90},
{30,50,55}};
System.out.println("\n 2-D Array marks1");
display(marks1,5,3);
System.out.println("\n 2-D Array marks2");
display(marks2,5,3);
System.out.println("\n 2-D Array marks3");
display(marks3,5,3);
System.out.println("\n 2-D Array marks4");
display(marks4,5,3);
System.out.println("\n 2-D Array marks5");
display(marks5,5,3);
}
static void display(int arr[][],int rows,int cols)
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
System.out.println(arr[i][j] + "\t");
}
System.out.println(arr[i][j] );
}
}
}

(coading4 for practical)


import java.io.*;
class Array2DTable
{
public static void main(String[]args)
{
int rows=3,columns=5;
int[][]marks=new int[rows][columns];
//initializing the array elements using for loop
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
marks[i][j]=i+j;
}
}
//printing the first three rows of marks array
System.out.println("first three rows are: ");
for(int i=0;i<3;i++)
{
for(int j=0;j<columns;j++)
{
System.out.println(marks[i][j]+ " ");
}
System.out.println();
}
}
}

(coading5 for practical)


import java.io.*;
import java.util.*;
class Code2DArray
{
public static void main(String[]args)
{
int[][] scores=new int[2][2];
//Initializing array element at position[0][0]
//0th row and 0th column
scores[0][0]=15;
//Initializing array element at position[0][1]
//0th row and 1st column
scores[0][1]=23;
//Initializing array element at position[1][0]
//0th row and 1st column
scores[1][0]=30;
//Initializing array element at position[1][1]
//1st row and 1st column
scores[1][1]=21;
//printing the array elements individually
System.out.println("scores[0][0]=" + scores[0]
[0]);
System.out.println("scores[0][1]=" + scores[0]
[1]);
System.out.println("scores[0][0]=" + scores[1]
[0]);
System.out.println("scores[1][1]=" + scores[1]
[1]);
//printing 2D array using
Arrays.deepToString() method
System.out.println("Printing 2D array using
Arrays.deepToString() method");

System.out.println(Arrays.deepToString(scores));
}
}

You might also like