Repository navigation
Expand file tree
/
Copy pathArrayBasics.java
More file actions
54 lines (40 loc) · 1.39 KB
/
Copy pathArrayBasics.java
File metadata and controls
54 lines (40 loc) · 1.39 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
49
50
51
52
53
54
package Arrays;
import java.util.stream.IntStream;
public class ArrayBasics {
public static void main(String[] args) {
//Syntax
//datatype[] variable_name = new datatype[size];
//Store 5 roll numbers:
int[] array = new int[5];
//or directly
int[] rollNumbers = {1,2,3,4,5};
System.out.println(rollNumbers.length);
//Integer Array
int[] ros;//declaration of array. ros is getting defined in the stack
ros = new int[5];//Initialisation: Object here is being created in the memory(heap)
// System.out.println(rollNumbers[2]);
//String Array
String[] names = new String[4];
// String[] names = {};
// System.out.println(names[1]);
int indexOfObjectToBeRemoved = 3;
array = removeTheElement(rollNumbers, indexOfObjectToBeRemoved);
System.out.print(array.length);
}
static int[] removeTheElement(int[] arr, int index)
{
// If the array is empty
// or the index is not in array range
// return the original array
if (arr == null
|| index < 0
|| index >= arr.length) {
return arr;
}
// return the resultant array
return IntStream.range(0, arr.length)
.filter(i -> i != index)
.map(i -> arr[i])
.toArray();
}
}