Array Java
Array Java
data[0][0] = "Ram";
data[0][1] = "MCA";
data[1][0] = "Shyam";
data[1][1] = "BCA";
}
}
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]);
}
}
}
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];
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];
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
System.out.println(arr[0][0]);
System.out.println(arr[0][1]);
}
}
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.