package com.codegym.task.task07.task0707;
import java.util.*;
/*
What sort of list is that?
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
ArrayList<String> string = new ArrayList<String>(5);
string.set(0,"r1");
string.set(1,"r2");
string.set(2,"r3");
string.set(3,"r4");
string.set(4,"r5");
System.out.println(string.size());
for (int i=0;i<string.size();i++){
System.out.println(string.get(i));
}
}
}
Why the program doesn't work ? it passes when i use add() but not with set(). Throws array-out-of bounds exception. The list isn't getting initialized to capacity 5.
Resolved
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Serg
23 May 2019, 10:07
What does the code mean in this term?
What is it?
0
Janusz
7 December 2018, 12:35
It's the same with me.
Thanks for the hint with add - it is good.
+1
satya vath
21 November 2018, 13:27
0
Roman
6 September 2018, 10:46
Try to run the code:
0
Ankush Rajput
6 September 2018, 11:04
tried that but it was showing zero.
0
Roman
6 September 2018, 11:25
Yes, your list doesn't have elements. And you cannot use set method.
+2