-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC48_UpdateArrayListElement.java
More file actions
36 lines (27 loc) · 1.1 KB
/
C48_UpdateArrayListElement.java
File metadata and controls
36 lines (27 loc) · 1.1 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
import java.util.ArrayList;
import java.util.Scanner;
public class C48_UpdateArrayListElement {
public static void main(String[] args) {
Name.info(); // Print name and enrollment number
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Yellow");
colors.add("Orange");
Scanner sc = new Scanner(System.in);
System.out.println("Original list: " + colors);
System.out.print("Enter the index of element to update: ");
int index = sc.nextInt();
sc.nextLine(); // consume newline
if (index >= 0 && index < colors.size()) {
System.out.print("Enter new color to update: ");
String newColor = sc.nextLine();
colors.set(index, newColor); // Update element
System.out.println("Updated list: " + colors);
} else {
System.out.println("Invalid index! Please enter index between 0 and " + (colors.size() - 1));
}
sc.close();
}
}