Docsity

Arrays, Strings and Vectors: Understanding Arrays in Java, Lecture notes of Java Programming

Toggle sidebar
bg1
Arrays, Strings and Vectors
Arrays
Arrays are the data structures which consist of collection of data elements of same data
type. Arrays have fixed length once they are declared their size cannot be changed. In JAVA
arrays are Objects Arrays can be declared and initialized.
e.g. Creation of an array has three steps
1. Declaring the array
2. Creating memory locations
3. Putting values into memory locations.
Declaration of an Array
Syntax
type arrayname[];
type[] arrayname;
e.g.
int no[ ];
float[ ] per;
float marks[ ];
Creation of Arrays
array name = new type[size];
e.g.
no=new int[5];
per=new float[5];
Array Initialization
Type arrayname[ ]={ list of values};
Int no[ ]={10,20,30,40,50};
pf3
pf4

Partial preview of the text

Download Arrays, Strings and Vectors: Understanding Arrays in Java and more Lecture notes Java Programming in PDF only on Docsity!

Arrays, Strings and Vectors Arrays Arrays are the data structures which consist of collection of data elements of same data type. Arrays have fixed length once they are declared their size cannot be changed. In JAVA arrays are Objects Arrays can be declared and initialized. e.g. Creation of an array has three steps

  1. Declaring the array
  2. Creating memory locations
  3. Putting values into memory locations. Declaration of an Array Syntax type arrayname[]; type[] arrayname; e.g. int no[ ]; float[ ] per; float marks[ ]; Creation of Arrays array name = new type[size]; e.g. no=new int[5]; per=new float[5]; Array Initialization Type arrayname[ ]={ list of values}; Int no[ ]={10,20,30,40,50};

Array Length We can get the length of an array by using arrayname.length. e.g: int var= no.length; JAVA supports single dimentional,two dimentional ,multidimentional and variable size arrays. Program to sort an array of integer numbers import java.io.*; class sort { public static void main(String args[]) { int a[]={14,32,25,17,29}; int i,j,temp; System.out.println(" elements before sorting are: "); for(i=0;i<5;i++) System.out.print("\t"+a [i]); for(i=0;i<5;i++) for(j=0;j<4-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=tem p; } } System.out.println(" elements after sorting are: "); for(i=0;i<5;i++) System.out.print("\t"+a[i]); } }