-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray1.java
More file actions
53 lines (40 loc) · 1.41 KB
/
Copy patharray1.java
File metadata and controls
53 lines (40 loc) · 1.41 KB
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
import java.util.*;
class Array1 {
private int[] array;
// Parameterized constructor to initialize the array
public Array1(int n)
{
array = new int[n];
System.out.println("Array memory location: " + System.identityHashCode(array));
}
// Method to fill the array with user input
public void createArray()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter " + array.length + " elements:");
for (int i = 0; i < array.length; i++) {
array[i] = sc.nextInt();
}
System.out.println("First element memory location: " + System.identityHashCode(array[0]));
System.out.println("Last element memory location: " + System.identityHashCode(array[array.length - 1]));
}
// Method to print the array
public void printArray() {
System.out.println("Array elements are:");
for (int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = sc.nextInt();
// Create an instance of Array1 with the specified size
Array1 array1 = new Array1(n);
// Fill the array with user input
array1.createArray();
// Print the array
array1.printArray();
}
}