Results 1 to 7 of 7

Thread: ArrayList

  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default ArrayList

    package firstList;


    import java.util.ArrayList;
    import java.util.Collections;

    class Aa implements Comparable
    {

    int i;
    public Aa(int i)
    {
    this.i = i;
    }

    public String toString()
    {
    return "i->"+i;
    }
    @Override
    public int compareTo(Object o)
    {
    return i-((Aa)o).i;
    }
    }
    public class D extends ArrayList
    {

    public static void main(String[] args)
    {
    D d = new D();
    ArrayList list = new ArrayList();
    list.add(new Aa(90));
    list.add(new Aa(143));
    list.add(new Aa(45));


    Collections.sort(list);
    System.out.println(list);


    ArrayList list1 = new ArrayList();
    list1.add(90);
    list1.add(100);
    list1.add(80);
    list1.add(70);
    list1.add(170);
    list1.add(270);
    list1.add(870);
    list1.add(70);

    d.removeRange(2, 5);

    System.out.println(list1);
    }
    }


    Hi,
    list1 consists of 8 elements,but when d.removeRange(2, 5); is executed java.lang.ArrayIndexOutOfBoundsException is raised

    i want to know y d.removeRange(2, 5); is resulting an exception though are 8 elements in the list1

    Thanks in Advance
    Daya
    Last edited by Dayanand; 03-09-2011 at 05:22 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default

    Because d is a completely different Object than list1. How many elements are in d?

    That's like asking, "Why is my red car out of gas when I just filled up my blue car?"
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Because d is a completely different Object than list1. How many elements are in d?

    That's like asking, "Why is my red car out of gas when I just filled up my blue car?"
    thanks for ur valuable solution.!!!!!!!!!!
    superb explaination.!!!!!!!

    Thanks
    Daya

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also when you implement compareTo doing n1 - n2 is bad. What if one value is Integer.MAX_VALUE or Integer.MIN_VALUE? It could loop around and give you the incorrect answer.

  5. #5
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Also when you implement compareTo doing n1 - n2 is bad. What if one value is Integer.MAX_VALUE or Integer.MIN_VALUE? It could loop around and give you the incorrect answer.
    thanks for ur valuable solution.!!!!


    Thanks
    Daya
    Last edited by Dayanand; 03-10-2011 at 06:05 AM.

  6. #6
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Dayanand View Post
    thanks for ur valuable solution.!!!!


    Thanks
    Daya
    how to implement Integer.MAX_VALUE ////Integer.MIN_VALUE?........ in compareTo()...........

    i tried it,but i got ClassCastException

    is it possible to implement.?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A quick easy solution is to use the built in int compareTo methods. Ill show an example using strings, and it works the same for ints

    The java classes have the compareTo method already designed if they implement comparable. Integer implements it so it has a compareTo method which you can simply use

    Java Code:
    public class MyClass implements Comparable<MyClass>{
      String s;
      public int compareTo(Object o){
        s.compareTo((MyClass)o.s);
      }
    }
    I know this is kind of spoon feeding, however; this can be found by anyone with a jdk so, here is the code directly from the java Integer class source code
    Java Code:
    public int compareTo(Integer anotherInteger) {
    	int thisVal = this.value;
    	int anotherVal = anotherInteger.value;
    	return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
        }
    Last edited by sunde887; 03-10-2011 at 06:31 AM.

Similar Threads

  1. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  2. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  3. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  4. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 03:34 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •