• 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
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Array list proper syntax

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what is the proper syntax for array list


some people use

ArrayList<String> cars = new ArrayList<String>();

but some use


ArrayList  cars = new ArrayList();

will it make any difference?
 
Marshal
Posts: 72407
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither is correct.

It is correct to declare such a reference by its interface, so that gives youYou will have problems accessing these two methods: 1 2, because it will need a cast.
Have a look at the Java™ Tutorials. You need to use an actual type parameter <String> to ensure that all its elements are the same type and to obviate any casting whenever you get(...) any elements. If you find an old (Java5/6/7) edition of the Java™ Tutorials, you can find a different description of generics. It is usually possible to miss out the second actual type parameter because the compiler can infer it 99% of the time.
The correct way to declare and initialise your List isYou may use a different constructor.

Moving you to a more appropriate forum.
 
Replace the word "snake" with "danger noodle" in all tiny ads.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic