Results 1 to 5 of 5
Thread: Help with Arrays
- 07-31-2008, 05:14 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 24
- Rep Power
- 0
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?
- 07-31-2008, 05:42 AM #2
Member
- Join Date
- Jul 2008
- Posts
- 19
- Rep Power
- 0
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.
- 07-31-2008, 02:37 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 24
- Rep Power
- 0
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.
- 07-31-2008, 03:10 PM #4
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)
- 08-01-2008, 05:12 AM #5
Member
- Join Date
- Jul 2008
- Posts
- 19
- Rep Power
- 0
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:
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.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 }
Now your array need not to be multidimensional.
Hope this will help you.
Similar Threads
-
arrays
By hasysf in forum New To JavaReplies: 12Last Post: 07-28-2008, 02:38 AM -
need help with arrays
By Jman in forum New To JavaReplies: 17Last Post: 07-21-2008, 02:34 AM -
Arrays
By bunbun in forum New To JavaReplies: 1Last Post: 04-09-2008, 02:24 AM -
new to arrays
By jimJohnson in forum New To JavaReplies: 1Last Post: 04-08-2008, 02:45 PM -
2D-Arrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02-07-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks