Categories
discuss

How do I clone a generic List in Java?

I have an ArrayList<String> that I’d like to return a copy of. ArrayList has a clone method which has the following signature:

public Object clone()

After I call this method, how do I cast the returned Object back to ArrayList<String>?

Answer

ArrayList newArrayList = (ArrayList) oldArrayList.clone();
Categories
discuss

Good 15 minute Java question to ask recent college graduate [closed]

When interviewing college coops/interns or recent graduates it helps to have a Java programming question that they can do on a white board in 15 minutes. Does anyone have examples of good questions like this? A C++ question I was once asked in an interview was to write a string to integer function which is along the lines of the level of question I am looking for examples of.

Answer

Some stuff that has showed up on SO:

  • IsPalindrome(string s)
  • ReverseWordsInString(string s): “I know java” –> “java know I”

Other stuff that springs to mind:

  • multiply a Vector with a Matrix (can this be done OO-Style?)
  • echo (yes, a simple clone of the unix tool)
  • cat (15 min should be enough, should weed out the clueless)
  • a simple container for ints. Like ArrayList. Bonus question: Generic?
Categories
discuss

How would you go about for switching a site from Prototype to jQuery

I have written a site in Prototype but want to switch to jQuery. Any ideas on how best make the switch?

Answer

Personally, I like to take things in steps, so I would start by using both, like this:

jQuery.noConflict();

// Put all your code in your document ready area
jQuery(document).ready(function($){
  // Do jQuery stuff using $
  $("div").hide();
});

// Use Prototype with $(...), etc.
$('someid').hide();

That way you don’t have to convert all your old code at once, but can start using jquery on new stuff, and migrate your old Prototype code when ever it’s convenient. I don’t know the size of your project, so I can’t say whether or not this applies to you, but Spolsky had a great article about “The big rewrite” and why it’s such a bad idea in Things you should never do, Part 1. It’s well worth a read!

For more on using jquery with Prototype, see Using jQuery with other libraries in the jquery docs.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..