• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Mock question for ArrayList ( Drag and drop )

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the question, and I am not sure the answer. I think I don't need so many spaces. Please help and thank you in advance.

Drag and drop the appropriate choices to fill the empty boxes provided in the following code so that its output is �121�

ArrayList<String> a = new ArrayList<String>();
a.add("1");
a.add("2");
a.add("1");

String[] s = a.___ ___ ___ ___ ___ ___ ___ ___; (total 8 space)
For (String str:s)
System.out.print(str);

Answers for choice:
New, String, [, toString, toObject, toList, ], 0, toArray, (, sort, ).
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What was the solution you had?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solution I had is:



Anonymous Arrays....
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! I guess I tried to fix the line w/o paying attention to the output. Andres is correct.
 
Andres Santana
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
M Krishnan, did you compile your answer... this code



Throws an Exception: java.lang.ClassCastException.

Apparently you cannot cast arrays.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool man.

Thank you Andres.

I am not sure if we need to know the API
public Object[] toArray(Object[] a) for SCJP1.5.

Any way, it is good to train my brain.
[ April 10, 2007: Message edited by: Sam Sunamin ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic