Results 1 to 7 of 7
  1. #1
    dironic88 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default How to use class objects in arrays

    Hey, i am working on a program where i need to get the "whole" object from the class "Borrehull" and put it into the main program.


    ////////////////////////////////////////////////////////////////////////////

    import java.io.Serializable;


    public class Borehull implements Serializable{
    private int x,y,d;

    public Borehull (int xkoord,int ykoord,int dim){
    x=xkoord;
    y=ykoord;
    d=dim;
    }

    public int getx() {
    return x;
    }

    public int getd() {
    return d;
    }

    public int gety() {
    return y;
    }
    }


    ///////////////////////////////////////////////////////////////////

    anybody who could try to explain how i can do that?

    i am getting the x and y from a mouseactionlistener with "getX()" and "getY()" and the dim is from a radiobutton wich i can adjust.

    all help will be much appriciated(:

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    hi. It's difficult to understand what you want, If I correctly understand you need store some objects in a array and can get and add object. Am I right?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    dironic88 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    yes that is correct

  4. #4
    dironic88 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    i need to get the objects from "borrehull" and store them in an array, so i can use it later in the program

  5. #5
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    It's Ok. There are two way, which you can use. First, if you know a number objects, which you want to store you just can use array. for example
    Java Code:
    Borehull[] stores = new Borehull[size];
    stores[0] = new Borehull();
    //.. and so on
    This is approach have better performance then use Collection framework, but it is not so flexible then collections. How you know array in java is immutable, so if you necessery add or remote some element from array you must create new array. It's bad.
    The other solve this is use collection frameworks. For example class ArrayList.
    Java Code:
    List<Borehull> lists = new ArrarList<Borehull>();
    lists.add(new Borehull());
    //and so on.
    The more info you can find in API by this classes.
    Skype: petrarsentev
    http://TrackStudio.com

  6. #6
    asmitarnd is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Red face

    u can refer to collection in java to store class instances.u can refer "List"
    collections in java

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

Similar Threads

  1. scanner objects with arrays again..
    By nevermiind in forum New To Java
    Replies: 18
    Last Post: 05-24-2010, 11:45 AM
  2. Objects and Arrays
    By bannow in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 07:15 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Replies: 1
    Last Post: 01-03-2010, 09:48 PM
  5. [SOLVED] Arrays of Objects with Subscripts
    By Sidmyre in forum New To Java
    Replies: 5
    Last Post: 12-12-2008, 02:18 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
  •