View RSS Feed

My Java Tips

  1. Bounding ArrayList

    by , 11-13-2011 at 12:45 PM (My Java Tips)
    Collections can be of a particular type i.e they are only allowed to hold objects of a defined type. This is called “bounded by”. For example:

    Java Code:
    ArrayList  arrayList  = new  ArrayList();
    Vector  vector  = new  Vector();

    We declared an ArrayList and a Vector both bounded by String. We cannot store objects other than String in these. Lets try to store an integer in Vector and see what happens.

    Java Code:
    Vector  vector  = new  Vector();
  2. Rotating Elements In a List using Collections.rotate Method

    by , 11-08-2011 at 07:22 PM (My Java Tips)
    Collections have very useful methods. Let us see how to rotate objects in a given list. The method "rotate" has two inputs, a list and a distance parameter which defines rotation distance. The distance can be positive, negative or zero. Objects in the list will be rotated by the specified parameter. Let us take an example to rotate objects of Car class.

    For example, suppose list comprises [t, a, n, k, s]. After invoking

    Collections.rotate(list, 1) ...