Results 1 to 6 of 6
  1. #1
    levis lover is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Help Needed.. Java Beginner

    It's about ArrayList

    In the documentation it says that the set and remove methods both return an object then how come we don't need to save the reference of it anywhere ?
    Java Code:
    public java.lang.Object remove(int);
    public java.lang.Object set(int, java.lang.Object);
    here are the two possibilities..
    Saving the refernece of the returned Objects
    Java Code:
    Team t3 = players.set(1,new Team("Rooney",12000,2));
    Team t4 = players.remove(2);

    It also works the other way around
    Java Code:
    players.set(1,new Team("Rooney",12000,2));
    players.remove(2);

    How ?
    Last edited by levis lover; 06-17-2010 at 05:33 AM.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    If I understand your question correctly, you don't have to store it as a reference because the item is stored in the ArrayList.
    Java Code:
    ArrayList<int> X = ArrayList<int>(2);
    // Here we can imagine X as something like, { null, null }
    X.set(0,5); // 5 is the object stored in the 0th index of the ArrayList X.
    // Here, X is { 5, null }
    int a = X.remove(0);
    // Once again, X is now { null, null }
    // However, when 5 was removed as the first element, it was stored in the variable 'a'.
    if (a == 5) { System.out.println("a == 5"); } // This will print.

    So hopefully I answered your question; the idea is that the new Team you created ("Rooney") is stored in the "players" ArrayList then removed later. The remove could look something like this (simplified):
    Java Code:
    public E remove(int index) {
          E obj = this.array[index];
          this.array[index] = null;
          return obj;
    }

    Good luck!

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Just because a method returns a value doesn't mean that you have to use that value. You are allowed to ignore it if you want, which is what your second examples are doing.

  4. #4
    levis lover is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    How can we call a constructor from a constructor?
    I did some search but could not understand the concept..

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Is this what you're referring to?:
    Java Code:
    public class SomeObj {
          public MyID;
          public SomeObj(int SomeID) {
                this.MyID = SomeID;
          }
          public SomeObj() {
                this(0); // Calls the first constructor with 0 as SomeID.
          }
    }

    If so, the idea is not to construct the object twice but simply call the constructor again as if it were a function:
    Java Code:
    public class SomeObj {
          public MyID;
          public SomeObj(int SomeID) {
                This_Could_Be_A_Construction_Function(SomeID);
          }
          public SomeObj() {
                This_Could_Be_A_Construction_Function(0);
          }
          // Both of the above functions call this function. They are both constructors, as shown:
          //    SomeObj A = new SomeObj(); // A.MyID == 0
          //    SomeObj B = new SomeObj(1); // B.MyID == 1
          public void This_Could_Be_A_Construction_Function(int SomeID) {
                this.MyID = SomeID;
          }
    }

    Hope that helps somewhat. If not, I can try to clarify further.
    Last edited by Zack; 06-17-2010 at 06:15 AM. Reason: Typo in 2nd code example

  6. #6
    levis lover is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Beginner in Java - need some help
    By ea09530 in forum Advanced Java
    Replies: 4
    Last Post: 04-05-2010, 11:47 PM
  2. java beginner
    By devstarter in forum New To Java
    Replies: 4
    Last Post: 03-03-2010, 08:39 AM
  3. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 04:29 AM
  4. Java Beginner needs help!!
    By Polyy in forum New To Java
    Replies: 4
    Last Post: 11-23-2008, 02:11 AM
  5. beginner to Java
    By notwist in forum New To Java
    Replies: 15
    Last Post: 04-18-2008, 09:41 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
  •