Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

why can not print List's content?

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:
import java.util.*;
class test{
public static void main(String[] args){
List l = Arrays.asList( int[]{1,2,3,4 }); //1
for (Object i:l)
{
System.out.println(i); //2
}
}
}
line 2 can not print l's content.except when i change line 1 to "List l = Arrays.asList(1,2,3,4)" or "List l = Arrays.asList(new Integer[]{1,2,3,4}),line 2 will print l's content,any one give some explaination?
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, first, your code does not compile. It is incorrectly written. It should have been like this:



Second, the list is actually storing the object, it is just that it is storing the whole array object ([I@3e25a5) and not, as you expected, one object per every item in the array.

Now, if you are using JDK 1.5 or JDK 1.6, then do it like this, it will behave as you were expecting:



I hope that helps!
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic