Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Can someone show me how to add an integer to a list please?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code so far. Lines 17,19,21, and 23 is the code giving me errors. Thanks in advance for your help! =D



 
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the Collection framework interfaces/classes, object types only can be processed ( add/remove/modify). Check the java documentation for more details.
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should help http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
 
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello George,

Are you sure you are using the correct "List"? From line 5 of your code, I see that you are creating a instance of List class. The conventional List container is however an interface and you should have gotten an error at this line itself as interfaces cannot be instantiated.

Can you please type out all your import statements too. That would help us give you further steps to solve the problem.

And welcome to the Ranch!

Regards,
Praveen.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Barney says you might not be using java.util.List.

You can add primitive ints to List using AutoBoxing.

 
Sheriff
Posts: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful with removing elements when using autoboxing. If I take John's example and call listOfInts.remove(2), this will not remove the element 2 but the 3rd element (which doesn't exist). It's not remove(E) that's being called but remove(int) - removal based on index.
 
Marshal
Posts: 70373
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Something about .... going for a swim. With this tiny ad ...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic