-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathJavaArray.java
179 lines (149 loc) · 6.38 KB
/
JavaArray.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class JavaArray {
public static void main(String[] args) {
// 1.
// Without a dimension or array elements, a compiler erro is thrown
// ( Variable must provide either dimension expressions or an array initializer )
// int[] arr = new int[];
// this is fine
int[] arr = new int[0];
System.out.println(arr.length); // 0
// invalid
// int[] arr2d[] = new int[][];
// valid
int[] arr2d[] = new int[5][];
// invalid
// int[] arr2d[] = new int[][5];
// valid
var arrVar = new int[10][];
// 2.
// No type with a var refernece will set the arraylist type to Object
var arrList1 = new ArrayList<>();
arrList1.add("20");
// Invalid ( Type mismatch: cannot convert from element type Object to String )
// for( String ele: arrList1 ) {
// System.out.println( ele );
// }
// Valid
for( Object ele: arrList1 ) {
System.out.println( ele );
}
// 3.
// If no generics are provided in the arraylist, then
// we can add any type of object into it.
/// Just the primitives are not allowed.
ArrayList arrlst = new ArrayList<>();
arrlst.add("Value");
arrlst.add(Boolean.TRUE);
System.out.println(arrlst);
// 4.
// we can't add to a list far into the index sequence
List<String> birds = new ArrayList<String>();
birds.add("hawk");
birds.add(1, "robin");
birds.add(1, "blue jay");
birds.add(3, "cardinal");
// throws runtime error
//birds.add(8, "cardinal");
System.out.println(birds);
// 5.
// Array to List using Arrays.asList create a fixed size backed list
// any changes made to array or list will be reflected at both the places
// If we try to remove the element from the array or the List then
// UnsupportedOperationException will be thrown.
String[] array = { "hawk", "robin" };
List<String> lst = Arrays.asList(array);
lst.set(0, "blue jay");
System.out.println(lst);
array[1] = "new";
System.out.println(lst);
// lst.remove(1); // UnsupportedOperationException
// 6.
// Creating immutable List using List.of(arr)
// We cannot change the size or the elements in such alist
List<String> immlst = List.of(array);
System.out.println(immlst);
array[0] = "Test";
System.out.println(Arrays.toString(array));
// immlst.set(0, "Change"); // UnsupportedOperationException
// immlst.remove(1); // UnsupportedOperationException
// 7.
// binarySearch
List<String> hex = Arrays.asList("30", "8", "3A", "FF");
Collections.sort(hex);
System.out.println(hex);
int x = Collections.binarySearch(hex, "8");
int y = Collections.binarySearch(hex, "3A");
int z = Collections.binarySearch(hex, "4F");
System.out.println(x + " " + y + " " + z); // 2 1 -3
// 8.
//
List<String> list = new ArrayList<>();
list.add("P");
list.add("O");
list.add("T");
List<String> subList = list.subList(1, 2); //Line n1
subList.set(0, "E"); //Line n2
System.out.println(String.join("", list));
// 9.
// A null array reference is considered lexicographically
// less than a non-null array reference.
System.out.println( java.util.Arrays.compare(new int [] {10}, null) ); // 1
// 10.
// Compare and Mismatch
// Two null array references are considered equal.
int[] a1 = null;
int[] a2 = null;
System.out.println( java.util.Arrays.compare(a1, a2) ); // 0
// null are not an array
// ( The method compare(boolean[], boolean[]) is ambiguous for the type Arrays )
// System.out.println( java.util.Arrays.compare(null, null) );
// But we can compare with an array
System.out.println( java.util.Arrays.compare(null, a1) ); // 0
int[] a3 = {1};
System.out.println( java.util.Arrays.compare(null, a3) ); // -1
System.out.println( java.util.Arrays.compare(a3, a3) ); // 0
// Compare has following algo
// int i = Arrays.mismatch(a, b);
// if (i >= 0 && i < Math.min(a.length, b.length))
// return Character.compare(a[i], b[i]);
// return a.length - b.length;
char [] arr1 = {'A', 'A'};
char [] arr2 = {'A', 'A', 'A', 'A'};
System.out.println(Arrays.compare(arr1, arr2)); // -2
System.out.println(Arrays.mismatch(arr1, arr2)); // 2
int [] array1 = {};
int [] array2 = {100, 200};
System.out.println(Arrays.compare(array1, array2)); // -2
// Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10
// char [] carr1 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k'};
// char [] carr2 = {'f', 'g', 'a', 'i', 'k'};
// System.out.println(Arrays.mismatch(arr1, 5, 10, arr2, 0, 5));
String [] str1 = {"A", "AA", "ABCD"};
String [] str2 = {"A", "AA", "ABCB"};
System.out.println(Arrays.mismatch(str1, str2)); // 2
System.out.println(Arrays.compare(str1, str2)); // 2
System.out.println("ABCD".compareTo("ABCB")); // 2
// System.out.println("ABCD".compareTo(null)); // NullPointerException
// 11.
// Unrelated array
int elements = 0;
Object [] objArr = {"A", "E", "I", new Object(), "O", "U"}; //Line n1
for(var obj : objArr) { //Line n2
elements++; //Line n3
if(obj instanceof String) {
continue;
} else {
break;
}
}
System.out.println(elements); // 4
String [] sarr = new String[7];
System.out.println(sarr); // L....@....
String[] s4 = { "Camel", null};
System.out.println( Arrays.compare(s4, s4));
}
}