• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom multi-field array

 
Leroy Lemon
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Leroy Lemon
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leroy Lemon
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic