-
Notifications
You must be signed in to change notification settings - Fork 0
/
E5 Updating array.java
50 lines (32 loc) · 1.47 KB
/
E5 Updating 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
import java.text.NumberFormat.Style;
import java.util.Arrays;
import java.util.Scanner;
// Update array
public class HelloWorld {
public static void main(String[] args) {
String[] menu = {"coffee","burger","chowmin","biriyani","chicken pokoda","cake"};
Scanner scanner = new Scanner(System.in);
System.out.println("Hi, What you will like to update ?");
String userInput = scanner.nextLine();
System.out.println("Enter the index: ");
int userIndex = scanner.nextInt();
scanner.close();
menu[userIndex] = userInput; // update the array.
System.out.println("Updated Array: " + Arrays.toString(menu));
// new value add to the menu array.
// create a new array which can store upto 10 elements.
String[] newMenu = new String[10];
// migrating old array to the new one.
for (int i = 0; i < menu.length; i++) {
newMenu[i] = menu[i];
}
// add new value to the new array.
newMenu[6] = "Pakhala"; // empty index will fieldup with "null".
System.out.println("New Menu Array: " + Arrays.toString(newMenu));
// the empty field will automatically filled with "Null" values.
}
}
// we can't increase length of a existing array.
// so we should create another array and merge the array to it.
// 1. The default value for String is null.
// 2. The default value for primitive types: int, double, long, char is 0.