Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

transfering variable arrays between classes??

 
hugh slater
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new to java and writing a program that includes reading a file to set an array for a combo box
my first idea was to have a seperate class to read the file and transfer the resulting array to the main class - however this seems not to be possible - return returns objects, integers, strings and doubles, but not in arrays. Is there any 'approved' method of doing this or should the method to read in the file reside in the main class???
thanks in advance
 
aminur rashid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi hugh
Instead of using arrays you can use any of these Vector / Hashtable/Enumeration/Collection
cheers
 
Kris Philippaerts
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.
First of all, using different classes and different objects is what's Java is all about. Putting this code in a different class will make it possible to re-use this code troughout your entire project. So you don't have to write the exact same code in 5 different places in your project.
Java has a lot of 'data-store' classes. Just go and look at the Collection interface in the Java docs. By clicking the known subinterfaces (List, Set, SortedSet), you will eventualy find all subclasses you can use as a collection. A little different, but oh so useful, is the HashMap. Go and read the docs, you'll see how to use it. And to use it, basically typ something like:
List getStringList() {
List lst = new Vector(); // because List is an interface
...
return lst;
}
void anOtherFunction() {
...
List lst = getStringList();
...
}
And finally, java can return arrays. For example:
String[] getStringArray() {
String[] str = new String[10];
...
return str;
}
void anOtherFunction() {
...
String myArray = getStringArray();
...
}
Hope this helps
Kris
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java arrays are object as well. use ObjectInputStream.readObject() and cast the result as your desired array type. If you serialized an array of ComboBox, try this
ComboBox[] boxes = (ComboBox[]) in.readObject();

Hope it helps.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hugh
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic