+ 1

Java code

How this code runs? isn't like this way? 4 2 3 remove(2) 4 3 Collections.sort(ListName) 3 4 System.out.println(ListName.get(0)*ListName.size()); 3*2 output:6 https://code.sololearn.com/c1A112xSHWIT/?ref=app

15th Jun 2019, 10:56 AM
Tzion
Tzion - avatar
3 ответов
+ 11
In simple words Remove 2 means remove index 2 Explantion ArrayList <Integer> list = new ArrayList <Integer>(); list.add(4); // index 0 list.add(2);// index 1 list.add(3);// index 2 list.remove(2);// remove 3 after this line list size will be 2; Collections.sort(list);// 2,4 after this line in index 0 replace with 2 and Index 1 replace with 4 System.out.print(list.get(0)*list.size());// 2 * 2 = 4;
15th Jun 2019, 11:16 AM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 7
remove(2) argument 2 is actually index of your arraylist. 4 (has index 0) 2 (has index 1) 3 (has index 2) So, you are removing 3, then you sort it (2,4) and then you multiply 2 with size of your arraylist which is 2. And your output is 2*2=4. If you want 6 to be your output, then remove element with index 1. remove(1)
15th Jun 2019, 11:09 AM
voja
voja - avatar
+ 1
Oh i thoughts removing elements 2... careless mistake
15th Jun 2019, 12:06 PM
Tzion
Tzion - avatar