-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizingArray.java
More file actions
40 lines (34 loc) · 1.22 KB
/
Copy pathResizingArray.java
File metadata and controls
40 lines (34 loc) · 1.22 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
import java.util.Scanner;
public class ResizingArray {
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
//STRING WRAPPER CLASS
System.out.print("Enter size for string array: ");
int size_str= sc.nextInt();
String str[]= new String[size_str];
System.out.print("Enter elements for string array: ");
for(int i=0; i<str.length; i++){
str[i]= sc.next();
}
for(int i=0; i<str.length; i++){
System.out.print(str[i] + " ");
}
System.out.println();
//RESIZING ARRAY
System.out.print("Enter new size for the array: ");
size_str= sc.nextInt();
str= new String[size_str];
//INPUT NEW ELEMENTS AS ARRAY SIZE IS UPDATED AND NEW
System.out.print("Enter new elements for the string array: ");
for(int i=0; i<str.length; i++){
str[i]= sc.next();
}
//PRINTING UPDATED SIZED ARRAY
for(int i=0; i<str.length; i++){
System.out.print(str[i] + " ");
}
System.out.println();
//ArrayIndexOutOfBoundsException ERROR AS ARRAY SIZE IS NO MORE 5, IT IS 3 NOW
System.out.println(str[4]);
}
}