Results 1 to 5 of 5
  1. #1
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default Help with Arrays

    I am working on a program where I need to put objects into an array through a four-argument constructor. I want to use a multi-dimensional array to store the data. The problem I am having is that everything I have read about arrays uses the same variable type for he entire array. I need to store 2 string variables, an int variable, and a double variable. How can I accomplish this?

  2. #2
    thusa is offline Member
    Join Date
    Jul 2008
    Posts
    19
    Rep Power
    0

    Default

    You can only define an array of one specific type no matter whether it's a multi-dimension or not.

    Like; Object[][] myAr = new Object[5][];

    In your situation, there are two options for you.
    1. Write a bean with all those four fields and use that as the type of your array.
    2. Define the array as Object type (show above), and store the values as you wish. But I do not recommend this since it would have type issues later.

    So best would be to write a small bean class with those for fields.

  3. #3
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    Number one, I have no idea what a "small bean class" is! And number two, i have already partially developed the program and all I can do now is create an array to hold the information. This small bean thing sounds really interesting, though. I sure would like to know what it is all about.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    Remove the word bean.
    Create a small class that holds all the different data types. Then you can make objects with the class that will hold your data and that you can put into an array of that type (type== your class)

  5. #5
    thusa is offline Member
    Join Date
    Jul 2008
    Posts
    19
    Rep Power
    0

    Lightbulb

    Exactly as Norm said, it will be a class with four variables to hold the values that you are planning to store in your multidimensional array.

    Eg:

    Java Code:
    public class ArrayData{
    
    private int index;
    private int count;
    private String name;
    private String group;
    
    //methods to get and set those values
    public void setIndex(int i) {
        this.index = i;
    }
    public int getIndex() {
        return this.i;
    }
    //add get and set methods for other three fields as well
    }
    Even though you have already done parts of the program, this will not raise issues, since you just have to create a new instance of ArrayData class and set those values there; then set this object into your array.

    Now your array need not to be multidimensional.

    Hope this will help you.

Similar Threads

  1. arrays
    By hasysf in forum New To Java
    Replies: 12
    Last Post: 07-28-2008, 03:38 AM
  2. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 03:34 AM
  3. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 03:24 AM
  4. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:45 PM
  5. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM

Posting Permissions

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