-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.java
68 lines (47 loc) · 1.97 KB
/
Array.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
import java.util.*;
import java.util.stream.IntStream;
public class Array{
public static void main(String[] args){
int[] arr0; // declare an array
int arr1[]; // declare an array
/*Note that we've only created an array reference.
No memory has been allocated to the array as the size is unknown,
and we can't do much with it.*/
int[] arr2 = {1,2,3,4};// The most common and convenient strategy declare and initialized
String[] arr3 = {"Volvo", "BMW", "Ford", "Mazda"}; // String Array
int[] arr4 = new int[10]; //declared an integer array object containing 10 elements,
arr4[4] = 500; //so you can initialize each element using its index value.
int[] arr5 = new int[]{13, 14, 15};//initialize the array using the new keyword as well
String[] arr6 = new String[]{"zelda", "link", "ganon"};//It works the same way.
int[] arr7 = IntStream.range(1, 11).toArray();//code creates an array of ten integers, containing the numbers 1 to 10:
// all Instream function 1)IntStream.rangeClosed()
// 2)IntStream.of()
// 3)IntStream.rangeClosed()
for(int i=0; i<arr2.length; i++){
System.out.println(arr2[i]);
}
for(int i=0; i<arr3.length; i++){
System.out.println(arr3[i]);
}
for(int i=0; i<arr4.length; i++){
System.out.println(arr4[i]);
}
for(int i=0; i<arr5.length; i++){
System.out.println(arr5[i]);
}
for(int i=0; i<arr6.length; i++){
System.out.println(arr6[i]);
}
for(int i=0; i<arr7.length; i++){
System.out.println(arr7[i]);
}
}
public static String[] getNames() {
return new String[]{"java", "python", "javascript"}; // Works
}
/*
public String[] getNames() {
return {"zelda", "link", "ganon"}; // Doesn't work
}
*/
}