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

Array Java

An array can store homogeneous data elements and is an object in Java. There are different types of arrays including single dimensional arrays which have one column and multiple rows, two dimensional arrays which can store data in a matrix format with rows and columns, and jagged arrays where the number of columns is not fixed to save space. Arrays can also be static with a fixed size declared at initialization or dynamic where the size is determined at runtime. The ArrayList is a collection that can dynamically adjust its size and is heterogeneous while lists are homogeneous.

Uploaded by

rachna lal
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Array Java

An array can store homogeneous data elements and is an object in Java. There are different types of arrays including single dimensional arrays which have one column and multiple rows, two dimensional arrays which can store data in a matrix format with rows and columns, and jagged arrays where the number of columns is not fixed to save space. Arrays can also be static with a fixed size declared at initialization or dynamic where the size is determined at runtime. The ArrayList is a collection that can dynamically adjust its size and is heterogeneous while lists are homogeneous.

Uploaded by

rachna lal
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 16

Using Arrays In Java :

An array can be defined as the collection of homogenous data


element which can be used to store one or more data for the
given types.
Array is an object in the Java.
An array can be categorised in the following ways :
Types of Arrays :
1. Single Dimensional Array : In this type of array we have only
one dimension i.e. one column and number of rows,
every row has a unique index number which is also known as
the subscript of the array.
The array subscript begins with 0 index onwards.
2. Two Dimensional or Multi Dimensional Array : In this type of
array we have more than one columns as the dimension in
which we can store our data.
it basically forms a matrix in which an individual cell is identified
by the intercetion of the row and column position i.e. [0,0] or
[0,1] onwards.
Ex.
class array2
{
public static void main(String args[])
{
String data[][] = new String[2][2];

data[0][0] = "Ram";
data[0][1] = "MCA";
data[1][0] = "Shyam";
data[1][1] = "BCA";

System.out.print("\tName : " + data[0][0]);


System.out.println("\tCourse : " + data[0][1]);
System.out.print("\tName : " + data[1][0]);
System.out.print("\tCourse : " + data[1][1]);

}
}
Ex. Using nested loop to print 2d array elements
class array2
{
public static void main(String args[])
{
String data[][] = new String[2][2];

data[0][0] = "Ram";
data[0][1] = "MCA";
data[1][0] = "Shyam";
data[1][1] = "BCA";

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(data[i][j]);
}

}
}

Ex. User input using for loop


import java.util.*;

class array2
{
public static void main(String args[])
{
String data[][] = new String[2][2];
Scanner obj = new Scanner(System.in);

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print("Enter the data: ");
data[i][j] = obj.nextLine();

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(data[i][j]);

}
}

Ex.
import java.util.*;

class array2
{
public static void main(String args[])
{
String data[][] = new String[2][2];

Scanner obj = new Scanner(System.in);

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
if(j%2==0)
System.out.print("Enter the name: ");
else
System.out.print("Enter the course: ");

data[i][j] = obj.nextLine();

}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.println(data[i][j]);

}
}

Ex.
import java.util.*;

class array2
{
public static void main(String args[])
{
String data[][] = new String[2][2];

Scanner obj = new Scanner(System.in);

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
if(j%2==0)
System.out.print("Enter the name: ");
else
System.out.print("Enter the course: ");
data[i][j] = obj.nextLine();

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print("\t" + data[i][j]);

}
System.out.println();
}
}
}
WAP program to accept 10 elements in an array and find the
following –
1. sum of all array elements
2. no. Of odd elements and event elements
3. sum of odd elements and event elements
4. display which total is higher odd or even
5. accept a no. From the user and check whether the
given no. Exists in the array or not and if the no. Is found
display its index position.
3. Jagged Array : A jagged array is also known as non-
rectangular array in which the dimension or number of columns
are not fixed, this can be varied in order to save the wastage of
unnecessary column space for the given row, because we
declare only the desired number of columns for the given row
of the table.
4. Dynamic and Static Array : In case of static array we have the
fixed number of array size, the size of the array is determined
during the declaration of the array.
We can also have the dynamic array in which the size of the
array is not fixed or determined during the declaration of the
array rather the size is determined during the runtime as per
the need.
Example of Static array :
int []x = new int[5]; //fixed array of int type having 5 elements
Example of Dyanmic Array :
main(String args[]) //dynamic array having no size or element
int a[];
int x = Integer.parseInt(scn.nextLine()); //10
a = new int[x]; //10 elements

Jagged Array: The concept of jagged array is to provide the space or


allocate the memory as per the need of the program rather than
allocating the unnecessary space like the rectangular array.
Example of jagged:
Example:
class array7
{
public static void main(String args[])
{
int [][]arr = new int[4][];
arr[0]= new int[2];
arr[1] = new int[3];
arr[2] = new int[4];
arr[3] = new int[1];
arr[0][0] = 11;
arr[0][1] = 12;

System.out.println(arr[0][0]);
System.out.println(arr[0][1]);

}
}

ArrayList – It is the collection of objects that can dynamically


adjust the size of the list i.e. the list will automatically grow is
the data is inserted and will automatically will shrink if the data
is removed from the list.
Important method of the arraylist-
Add() – to add new item in the list
Remove() – to remove the item from the list
contains() – to check for the item in the list returns true if found
otherwise false.
Size() – to return the no. of items in the list
Clear() – to clear all items from the list
Note: all the items in the list are of object type i.e. we can store
any data type, i.e. the arraylist will be heterogenous type.
Ex.
import java.util.*;

class array8
{
public static void main(String args[])
{
ArrayList a = new ArrayList();

a.add(1);
a.add(2);
a.add(3);

System.out.println(a.size());

a.add("A");
a.add("B");

System.out.println(a.size());

a.remove(0);

System.out.println(a.size());

}
}
List Collection – A list is like the arraylist but since it can be
made type specific so it is more useful because it will be
homogenous.
HashTable – it is the key value pair, i.e. in the every item will
have a unique key instead of the index i.e. 0,1,2
Since the key can be alphanumeric so the item will be more
meaning ful.

Country[0] = “Delhi”;
Country[1] = “Tokyo”;

Country[“IND”] = “Delhi”;
Country[“JPN”] = “Tokyo”;
All the collection classes and interfaces are defined in the
java.util pacakge.

You might also like