-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasics.java
More file actions
55 lines (43 loc) · 2.19 KB
/
Copy pathBasics.java
File metadata and controls
55 lines (43 loc) · 2.19 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
55
package Array;
import java.util.Arrays;
import java.util.Scanner;
public class Basics {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Syntax:
// datatype[] variable_name = new datatype[size];
// or, datatype[] var_name = {20, 35, 257, 45};
//it is a dynamic memory allocation (objects are created at runtime)
// datatype and reference_variable are created at compile time and are stored in stack memory.
// array objects are stored in heap memory
// In JLS (java language specification); they have mentioned that heap objects are not continuous.
// Example:
int[] arr = new int[5];
arr[0] = 45;
arr[1] = 23;
arr[2] = 78;
arr[3] = 549;
arr[4] = 91;
System.out.println(arr[3]); //array data of 3rd index will be printed
// each particular element of an array will be an object.
// this type of writing can be very repetitive so that we can use for loop
//like:
// input using for loop
System.out.print("Enter 5 integers to be stored in an array: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt(); // this will take input for every index value inside an array
}
// for (int i = 0; i < arr.length; i++) {
// System.out.print(arr[i]+ " "); // this will print every data in an array and loop will terminate after completing its size (or length)
// }
//enhanced array
for (int num: arr) { // for every element in array, print the array
System.out.print(num + " "); // here num represents element of the array
}
// System.out.println(arr[5]); //this will give index out of bound error
// use of Scanner for taking input data of an array.
//"arr.length" represents the length variable (length of an array i.e. depends on the size of an array) and for loop will take input accordingly.
// easiest way to print an array
System.out.println(Arrays.toString(arr)); // when you give array into it, it will convert into string and prints that string (use ctrl + keyword to look for the code)
}
}