aspose file tools*
The moose likes Java in General and the fly likes Custom multi-field array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Custom multi-field array" Watch "Custom multi-field array" New topic
Author

Custom multi-field array

Leroy Lemon
Greenhorn

Joined: Dec 15, 2005
Posts: 21
Hello,

I am trying to figure out what would be the best way to create an array (or vector) that has 2 types of fields. The first field is a string and the second field is a byte array.
I've looked at collections, but I'm not sure how I'd code a solution for this. I was thinking of making an object with the 2 elements and then vectoring a collection of the object. Does that make sense?

If anyone has any suggestions, I'd really appreciate it.

Thanks.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Although I certainly don't want to discourage you from encapsulating the data in an object if that is warranted, you may also want to take a look at the Map<K, V> interface and its concrete implementations, especially HashMap and TreeMap. Maps use key value pairs. Since Sun can probably explain them better than I can, here is a link to their tutorial on Maps.
[ March 17, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Leroy Lemon
Greenhorn

Joined: Dec 15, 2005
Posts: 21
Thanks for the tip Garrett, but I was thinking more along the line of a type array like you would do in VB. The map seems to limit my ability to do duplicate keys. I'm trying to store a byte array and a string identifier to tell me what is in the byte array. There will be more than one pair of string/byte[] elements.

I'm thinking the best thing to do is create a simple class of 2 arrays and create a collection. I think I have some learnin to do.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by Leroy Lemon:

I'm thinking the best thing to do is create a simple class of 2 arrays and create a collection.


Not quite, I don't think, if you're suggesting a class with two parallel arrays. Instead create a simple class with a String and a byte[] as members, and then store instances of that class in some predefined collection type.


[Jess in Action][AskingGoodQuestions]
Leroy Lemon
Greenhorn

Joined: Dec 15, 2005
Posts: 21
Thanks Ernest.

Here is what I did to solve my problem.

I created a simple class that had the 2 parameters.



And I created a vector to hold my collection...



When I need to add to the collection, I create an instance of the DataSet object and just add it to the vector.



Thanks to everyone for their help. Happy St. Paddy's day.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

That's great! Want to make it better?

Instead of setting the member variables explicitly, why not add a more useful constructor to the class?



Then you add a new one in one line, like this:

msgParts.add(new DataSet("disposition-notification", body));
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom multi-field array
 
Similar Threads
Calling Methods with strings??
error in programe
Casting issue
getting selected row in datatable
Array questions