-
Notifications
You must be signed in to change notification settings - Fork 0
/
arr_cls.java
111 lines (91 loc) · 3.72 KB
/
arr_cls.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.Scanner;
class arr_cls{
public static void main(String[] a){
// Declaring an array here
int arr[];
// Allocating the memory space to it
arr = new int[5];
// for taking input
Scanner scn = new Scanner(System.in);
// Initialising the values of the array
for(int i = 0;i < arr.length;i++){ // length keyword to get the length of the array
System.out.print("Enter a value for the element : ");
arr[i] = scn.nextInt();
}
System.out.println();
// Declaring an Array Literal
int[] array1 = new int[]{1,2,3,4,5,6,7};
System.out.print("Elements of an Array Literal : ");
// Iterating over this Array
for(int i = 0;i < array1.length;i++){
System.out.print(array1[i] + " ");
}
System.out.println();
// Defininga Multidimensional Array
int[][] array2 = new int[3][3];
// Getting the Rows and Columns of the Array
System.out.println("Rows of the Array : " + array2.length);
System.out.println("Column of the Array : " + array2[0].length);
// Initiating a Multi-Dimensional Array
for(int i = 0;i < array2.length;i++){
for(int j = 0;j < array2[0].length;j++){
System.out.print("Enter an element : ");
array2[i][j] = scn.nextInt();
}
System.out.println();
}
scn.close();
// Iterating over to the Array
System.out.println("Element of the Array are mentioned below : ");
for(int i = 0;i < array2.length;i++){
for(int j = 0;j < array2[0].length;j++){
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
// Cloning of 1D Array
int[] arr1 = arr.clone();
System.out.println("Printing the Array which is cloned : ");
for(int i = 0;i < arr1.length;i++){
System.out.print(arr1[i] + " ");
}
System.out.println();
// Passing the arrays to the function
sum(arr);
// Cloning Multidimensional Array
int[][] array3 = array2.clone();
System.out.println("Elements of Multidimensional Array : ");
for(int i = 0;i < array3.length;i++){
for(int j = 0;j < array3[0].length;j++){
System.out.print(array3[i][j] + " ");
}
System.out.println();
}
// Returning array from the methods or functions
int[] array4 = arr1();
System.out.println("\nElements of the array that is created by returning from a method : ");
for(int i = 0;i < array4.length;i++){
System.out.print(array4[i] + " ");
}
System.out.println();
// Creating Class Objects for Arrays
byte[] bytearray = new byte[3];
short[] shortarray = new short[3];
String[] string_array = new String[3];
// Printing the data to the user
System.out.println("Class of first bytearray created : " + bytearray.getClass());
System.out.println("Class of second shortarray created : " + shortarray.getClass());
System.out.println("Class of third string_array created : " + string_array.getClass());
System.out.println("SuperClass of third string_array created : " + string_array.getClass().getSuperclass());
}
public static void sum(int[] a){
int result = 0;
for(int i = 0;i < a.length;i++){
result += a[i];
}
System.out.println("Sum of all the digits of the array is : " + result);
}
public static int[] arr1(){
return new int[] {34,55,65};
}
}