Results 1 to 20 of 20
  1. #1
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default Array of Objects

    I'm trying to write a method to add elements in an array of objects?
    I can do it with primitive types but not with Objects?
    What am I doing wrong?


    public void addElement (Object element)
    {

    int k,j;
    for(j=0; j<size; j++) // find where it goes
    if( (objects[j]) > element) // (linear search)
    break;
    for(int k=size; k>j; k--) // move bigger ones up
    objects[k] = objects[k-1];
    objects[j] = element; // insert it
    size++; // increment size

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    what does it mean when one object is greater than another? Doesn't mean much to me. Are these objects Comparable objects? If so then perhaps you can pass a Comparable parameter and use the compareTo method within the if test.

  3. #3
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    rafb.net/p/6jLV2K43.html
    here is my complete code!
    Let's say they are not comparable.. I just simply want to add an object to an array of objects!
    Could you tell me how to do it?
    What I have their works with numbers but not objects!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you objects are not comparable, then I see no way to order your objects, and so you may as well add each new one to the end of the filled slots.

    Myself, I'd use a Collection such as an ArrayList and in effect let Sun do all the hard work for me.

  5. #5
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Could you tell me how to add each element to the end of the list?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again, I'd just use ArrayList, end of story. :)

    but if you do use an array of object, what is it initially filled with? (if you don't know it's easy to create a small app to test it -- why not give it a go?).

  7. #7
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    the point is that I am new and I want to learn creating an array of objects and I don't want to use ArrayList ;)

    I chose to fill my array of objects with strings as you can see in my test class which doesn't compile!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    the point is that I am new and I want to learn creating an array of objects and I don't want to use ArrayList
    OK, I'll buy that, but it still doesn't change my innate laziness, not one bit.

    I chose to fill my array of objects with strings as you can see in my test class which doesn't compile!
    Then create a new test class. Do me a favor and create an array of 10 Objects, then in a for loop do a println on each object. What comes up?

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i started looking at the code, but before i realized it, i had made so much mods to it already... (and i am lazy as well, so i'll stop here.)
    Java Code:
    public class MyArray_Test{
      public static void main(String[] args){
        int size = 5;
        int capacity = 5;
        MyArray myArray = new MyArray(capacity);
    
        myArray.addElement("element1");
        myArray.addElement("element2");
        myArray.addElement("element3");
        myArray.addElement("element4");
        myArray.addElement("element5");
    
        myArray.display();
      }  // end main()
    }  // end class StudentTest
    
    
    class MyArray {
      private Object[] objects; // the array containing the objects
    
      public MyArray (int cap){
        objects = new Object [cap];
      }
    
      [B]public void addElement (Object element){
        Object[] a = new Object[objects.length+1];
        for(int i=0; i<objects.length; i++){
          a[i] = objects[i];
        }
        a[a.length-1] = element;
        
        // SORT STUFF HERE
        
        objects = a;
      }[/B]
    
      public void display(){
        for (Object o : objects)
          System.out.println((String)o + " ");
      }
    }
    ** Do you need me to explain the code? I'm hoping not... **
    Last edited by angryboy; 02-03-2009 at 05:03 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    This line of code:

    Java Code:
    if( (objects[j]) > element)
    Is meaningless and will not compile. An object can be an instance of any class. Does it make sense to say "if blue is larger than socket"? Because that's what that if statement amounts to.

    What are you trying to accomplish with that statement?

  11. #11
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    I just simply want to add an object to an array of objects!
    you can use Vector instead of the array

  12. #12
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    as stated by the OP, he(/she) only want to know if its possible. and how? not to actually use it. (and i hope not.)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  13. #13
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default Array of Objects

    thanks for this part of the code.
    I will play around with it and figure it out :)
    could you give me a hint on remove as well plz?

    Quote Originally Posted by angryboy View Post
    i started looking at the code, but before i realized it, i had made so much mods to it already... (and i am lazy as well, so i'll stop here.)
    Java Code:
    public class MyArray_Test{
      public static void main(String[] args){
        int size = 5;
        int capacity = 5;
        MyArray myArray = new MyArray(capacity);
    
        myArray.addElement("element1");
        myArray.addElement("element2");
        myArray.addElement("element3");
        myArray.addElement("element4");
        myArray.addElement("element5");
    
        myArray.display();
      }  // end main()
    }  // end class StudentTest
    
    
    class MyArray {
      private Object[] objects; // the array containing the objects
    
      public MyArray (int cap){
        objects = new Object [cap];
      }
    
      [B]public void addElement (Object element){
        Object[] a = new Object[objects.length+1];
        for(int i=0; i<objects.length; i++){
          a[i] = objects[i];
        }
        a[a.length-1] = element;
        
        // SORT STUFF HERE
        
        objects = a;
      }[/B]
    
      public void display(){
        for (Object o : objects)
          System.out.println((String)o + " ");
      }
    }
    ** Do you need me to explain the code? I'm hoping not... **

  14. #14
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i would code it the same as you had, by passing an index. then use a loop to copy the array to a new array. but if j==index, then continue;. also size-- and cap--.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  15. #15
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    ok thanks angryboy. :)

    do you think I need to add any other methods like ensureCapacity or a search() method?

  16. #16
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Webuser,
    I need to implement a class of my own. can't use vector.

  17. #17
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    can you tell me why I am getting these errors?!!
    non-static method addElement(java.lang.Object) cannot be referenced from a static context MyArray.addElement("element5");
    MyArray.java:168: non-static method display() cannot be referenced from a static context MyArray.display();

  18. #18
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    addElement isn't a static method, but calling it as MyArray.addElement is using it in static context. You want an instance of the array with MyArray arr = new MyArray() and do arr.addElement(...)

  19. #19
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by sfe23 View Post
    ok thanks angryboy. :)

    do you think I need to add any other methods like ensureCapacity or a search() method?
    sure, but don't go overboard w/ this. you are working w/ objects, so you'll have to create different ways to sort/search/parse/etc... it.

    Quote Originally Posted by emceenugget View Post
    addElement isn't a static method, but calling it as MyArray.addElement is using it in static context. You want an instance of the array with MyArray arr = new MyArray() and do arr.addElement(...)
    what he said. i made some changes to your main to make it work. so you can either make it static or use my main().
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  20. #20
    sfe23's Avatar
    sfe23 is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Quote Originally Posted by angryboy View Post
    as stated by the OP, he(/she) only want to know if its possible. and how? not to actually use it. (and i hope not.)

    You mean She :)

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM
  3. Array of Objects
    By bluefloyd8 in forum New To Java
    Replies: 5
    Last Post: 01-22-2008, 06:27 PM
  4. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 AM
  5. array of objects
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:24 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
  •