Answered Questions

  • Arrays/Linked List

    When and on what conditions will you decide whether to use linked lists or arrays, also Which process is fastest and which is slowest in arrays as well as linked list i.e, sorting, searching, add, delete etc.

    Star Read Best Answer

    Editorial / Best Answer

    Aqan12  

    • Member Since Apr-2010 | Jun 13th, 2010


    Memory:
    When you create an array you allocate all the memory while initializing the array, so if you had an array of 1000 Objects, you'd allocate space to hold 1000 object references as soon as you initialize the array.

    LinkedList on the other hand extends dynamically as the objects are added to the linked list.

    Accessing Elements:
    Arrays are useful if you are going to access elements using the index. if you need to access the 100th element you can directly access it by a[100].

    To access 100th element in a LinkedList you'll have to traverse 99 elements to reach the 100th element.

    LinkedList can handle addition/deletion of elements better then arrays and that's one of the most powerful feature of LL over arrays.

    Adding/Inserting Elements:
    Array is fixed size; to add a number of elements greater then the size of the array you'd have to copy the array into a new bigger array.

    To insert an element in the middle of an array all the elements after the inserted element would have to moved up one index.

    LinkedList can grow dynamically.

    Deleting Elements:
    To delete an element from Array you'd have to move down all the elements after the deleted element.

    LinkedList elements can be deleted dynamically.

    To summarize, LinkedList can handle manipulation of data structure much better and efficiently manage memory but arrays have better accessibility when accessing elements by index.
    Search and Sort would be similar performance on both.

  • Mutithreading

    How synchronization can be done for multithreading environment without using the "synchronized" keyword?

    Aqan12

    • Apr 13th, 2010

    Use java.util.concurrent package Lock mylock;try{ mylock.lock(); //critical section}finally{ mylock.unlock();} Also: try a lock with a timeout trylock() wait for some condition: condition.await() these could come handy to avoid the deadlock situations.

  • When you have an object passed to a method and when the object is reassigned to a different one, then is the original reference lost

    Aqan12

    • Jun 4th, 2010

    Primitives are always passed by value.Objects: Passed by value of reference. i.e. a copy of reference is passed into the method. Any changes to the reference itself will not be visible outside the met...

    Scorpio_

    • May 7th, 2010

    Mr Ashutosh Gupta,Java is pass-by-value. For primitives, you pass a copy of the actual value. For references to objects, you pass a copy of the reference. You never pass the object. All objects are stored on the heap. Always http://www.javaranch.com/campfire/StoryPassBy.jsp