Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Why does the following program output 2, 1 instead of 1,0

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.ArrayList;
import java.util.List;

/* What will be the result of compiling and executing Test class?
  a) An exception is thrown at runtime
  b) [1,0]                                 // Incorrect
  c) Compilation error
  d) [2,1]                                 // Correct                                        */


public class _20_ArrayList_Remove_Ele_Question {

   public static void main(String [] args) {
       List<Integer> list = new ArrayList<Integer>();
       list.add(new Integer(2));
       list.add(new Integer(1));
       list.add(new Integer(0));
       
       list.remove(list.indexOf(0));

       System.out.println(list);
   }
}


I know that Integer is a wrapper class but we are removing the element which is at index 0, right? Well, obviously not, please kindly explain. Thanks for any help that you can give.
 
Master Rancher
Posts: 4081
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autoboxing strikes again.  
 
Saloon Keeper
Posts: 6796
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a little exercise for you: replace "indexOf" with "get", and then run the program again. Then, replace "remove(list.get(0))" with "remove(0)", and run the code again. Once you've thought about the result, head over to java.util.List to read up on the difference between these.
 
Clemonte Johnstone
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replacing indexOf with get gives me 1,0
I know what autoboxing is. However, in this instance the list is of type Integer and we are adding elements of type Integer so where is the autoboxing?

Are you saying that indexOf returns us int 2 which gets autoboxed to Integer resulting in what...?

Please explain?
 
Norm Radder
Master Rancher
Posts: 4081
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

where is the autoboxing?


 list.remove(list.indexOf(0));  <<  0 autoboxed to Integer(0)


Read the API doc for the indexOf method.  It takes an Object not an int
indexOf returns a 2 which will remove the 0

I wonder how many hours have been spent tracking down an autobox error vs the time saved by not having to type new Integer()
 
Tim Moores
Saloon Keeper
Posts: 6796
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Norm missed that the objective is to remove the element at index 0, not the element 0 at wherever index it may occur. Thus indexOf is the wrong method to use.

but we are removing the element which is at index 0, right?


No, that's not what that method does, at least not the way you're using it.

Re-read my previous post, it shows two ways to fix this (one of them being way better than the other).
 
Clemonte Johnstone
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:I think Norm missed that the objective is to remove the element at index 0, not the element 0 at wherever index it may occur. Thus indexOf is the wrong method to use.

but we are removing the element which is at index 0, right?


No, that's not what that method does, at least not the way you're using it.

Re-read my previous post, it shows two ways to fix this (one of them being way better than the other).



I've understood what it is doing mate. It is removing the 0 which is at index 2. Thanks
 
Norm Radder
Master Rancher
Posts: 4081
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the objective is to remove the element at index 0,


Why would the code use indexOf to find where a item was located in the list if the intention was to remove the item at index 0?

That is one of the beauties of comments - they explain what the intention of code is.  Otherwise it can be a guessing game.
 
Clemonte Johnstone
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

the objective is to remove the element at index 0,


Why would the code use indexOf to find where a item was located in the list if the intention was to remove the item at index 0?

That is one of the beauties of comments - they explain what the intention of code is.  Otherwise it can be a guessing game.



I dont know who thought up this question but so long as I understand how the code works then that is fine
 
Tim Moores
Saloon Keeper
Posts: 6796
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Why would the code use indexOf to find where a item was located in the list if the intention was to remove the item at index 0?


That's what Mark came here to learn
 
Men call me Jim. Women look past me to this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic