Professional Documents
Culture Documents
import java.util.*;
class arrays
{
void one_array()
{
int one_arr[]=new int[3] ;
Scanner s=new Scanner(System.in);
System.out.println("************************ 1-DIMENSIONAL ARRAY *****
***********************");
System.out.println("Enter Elements for 1-Dimensional Array : ");
for(int i=0;i<one_arr.length;i++)
{ one_arr[i]=s.nextInt(); }
for (int i = 0; i < one_arr.length; i++) {
System.out.print(one_arr[i] + " ");
}
}
void two_array()
{
Scanner s=new Scanner(System.in);
int two_arr[][]= new int [3][3];
System.out.println("************************ 2-DIMENSIONAL ARRAY ****
************************");
System.out.println("Enter Elements for 2-Dimensional Array : ");
for(int i=0;i<3;i++)
{
for(int j=0;j<two_arr[i].length;j++)
{
two_arr[i][j]=s.nextInt();
}
}
for (int i = 0; i < two_arr.length; i++) {
for (int j = 0; j < two_arr[i].length; j++)
System.out.print(two_arr[i][j] + " ");
System.out.println();
}
}
void jagged_array()
{
Scanner s=new Scanner(System.in);
int jagged_arr[][] = new int[3][];
jagged_arr[0] = new int[4];
jagged_arr[1] = new int[1];
jagged_arr[2] = new int[2];
System.out.println("************************ JAGGED ARRAY ************
****************");
System.out.println("Enter Elements for Jagged Array : ");
for(int i=0;i<3;i++)
{
for(int j=0;j<jagged_arr[i].length;j++)
{
jagged_arr[i][j]=s.nextInt();
}
}
for (int i = 0; i < jagged_arr.length; i++) {
for (int j = 0; j < jagged_arr[i].length; j++)
System.out.print(jagged_arr[i][j] + " ");
System.out.println();
}
}
}
public class array_types
{
public static void main(String []args)
{
arrays arr=new arrays();
arr.jagged_array();
arr.two_array();
arr.one_array();
}
}
Array Functions :
import java.util.*;
class array_methods
{
void asList_method()
{
int intArr[] = { 10, 20, 15, 22, 35 };
System.out.println("Integer Array as List: " + Arrays.asList(intArr));
// To convert the elements as List
}
void binarySearch_method()
{
int intArr[] = { 10, 20, 15, 22, 35 }; // Get the Array
Arrays.sort(intArr);
int intKey = 22;
System.out.println(intKey + " found at index = "+ Arrays.binarySearch(
intArr, intKey)); // Print the key and corresponding index
System.out.println(intKey + " found at index = "+ Arrays.binarySearch(
intArr, 1, 3, intKey)); // binarySearch(array, fromIndex, toIndex, key, Compar
ator)
}
void compare_method()
{
int intArr[] = { 10, 20, 15, 22, 35 };
int intArr1[] = { 10, 15, 22 }; // Get the second Array
System.out.println("Integer Arrays on comparison: " + Arrays.compare(i
ntArr, intArr1)); // To compare both arrays
System.out.println("Integer Arrays on comparison: " + Arrays.compareUn
signed(intArr, intArr1));
}
void copyOf_method()
{
int intArr[] = { 10, 20, 15, 22, 35 }; // Get the Array
System.out.println("Integer Array: " + Arrays.toString(intArr)); //
To print the elements in one line
System.out.println("\nNew Arrays by copyOf:\n");
System.out.println("Integer Array: " + Arrays.toString(Arrays.copyOf(i
ntArr, 10)));
System.out.println("\nNew Arrays by copyOfRange:\n");
System.out.println("Integer Array: "+ Arrays.toString(Arrays.copyOfRan
ge(intArr, 1, 3))); // To copy the array into an array of new length
}
void deepEquals_method()
{
int intArr[][] = { { 10, 20, 15, 22, 35 } }; // Get the Arrays
int intArr1[][] = { { 10, 15, 22 } }; // Get the second Arrays
System.out.println("Integer Arrays on comparison: "+ Arrays.deepEquals
(intArr, intArr1)); // To compare both arrays
}
void deepHashCode_method()
{
int intArr[][] = { { 10, 20, 15, 22, 35 } }; // Get the Array
System.out.println("Integer Array: "+ Arrays.deepHashCode(intArr)); /
/ To get the dep hashCode of the arrays
}
void deepToString_method()
{
int intArr[][] = { { 10, 20, 15, 22, 35 } }; // Get the Array
System.out.println("Integer Array: "+ Arrays.deepToString(intArr)); //
To get the deep String of the arrays
}
void equals_method()
{
int intArr[] = { 10, 20, 15, 22, 35 }; // Get the Arrays
int intArr1[] = { 10, 15, 22 }; // Get the second Arrays
System.out.println("Integer Arrays on comparison: " + Arrays.equals(in
tArr, intArr1)); // To compare both arrays
}
void fill_method()
{
int intArr[] = { 10, 20, 15, 22, 35 }; // Get the Arrays
int intKey = 22;
Arrays.fill(intArr, intKey);
System.out.println("Integer Array on filling: "+ Arrays.toString(intAr
r)); // To fill the arrays
}
void hashCode_method()
{
int intArr[] = { 10, 20, 15, 22, 35 }; // Get the Array
System.out.println("Integer Array: "+ Arrays.hashCode(intArr)); // T
o get the hashCode of the arrays
}
void mismatch_method()
{
int intArr[] = { 10, 20, 15, 22, 35 }; // Get the Arrays
int intArr1[] = { 10, 15, 22 }; // Get the second Arrays
System.out.println("The element mismatched at index: " + Arrays.mismat
ch(intArr, intArr1)); // To compare both arrays
}
void parallelSort_method()
{
int intArr[] = { 10, 20, 15, 22, 35 }; // Get the Array
Arrays.parallelSort(intArr); // To sort the array using parallelSort
System.out.println("Integer Array: " + Arrays.toString(intArr));
}
}
public class array_functions
{
public static void main(String[] args)
{
array_methods am = new array_methods();
am.asList_method();
am.binarySearch_method();
am.compare_method();
am.copyOf_method();
am.deepEquals_method();
}
}
3.ForEach Loop
class foreach_age
{
public static void main(String[] args) {
int ages[] = {15, 18, 16, 17, 14, 12, 13, 20, 22, 25};
int ctr = 0, sum = 0;
System.out.print("Ages of the group are : "); for (int x : ages)
{
System.out.print(x+" ");
}
for (int x : ages)
{
if (ctr == 5) break; sum += x;
ctr += 1;
}
System.out.println("\nSum of age of first 5 people of the group = " + sum);
}
}
public class instance_block {
instance_block(){System.out.println("Constructor is Invoked");}
{System.out.println("Instance Initializer Block is Invoked");}
public static void main(String args[]){
instance_block ibb1=new instance_block();
instance_block ibb2=new instance_block();
}
}