function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MarkInAtlantaMarkInAtlanta 

MultiColumn Array

Kind of a simple issue (i think). I have an Apex Class that i want to build a multi-column array to use later. 

something along the line of:  

List<Id, string, double> 3ColumnList = new List<Id, string, double>(); 

This is not legal since the list wants a single type..  How would one handle an internal table or array like this in Apex? 
 
Im trying to get some processing out of the loop and  handle it in a more bulkified manner. I dont have an object that matches the array description although i guess I could "borrow" an existing object type..  Seems like a hack to do that though..   



 
Best Answer chosen by MarkInAtlanta
JeffreyStevensJeffreyStevens
There are a couple of things that you could look at.

Learn about Maps.  Maps are an orderd, collection of a key/value pair.  So you could create two maps, one to hold the id/string key/value pair, and one to hold the id/decimal key/value pair.  They would be defined like this...
// Define two maps
map<id,string> mIdToString = new map<id,string>();
mIdtoString.put(IdFieldAlreadyDefinedAndPopulated , StringFieldAlreadyDefinedAndPopulated) ;

map<id,decimal> mIdToDecimal = new map<id,decimal>();
mIdToDecimal.put(idFieldAlreadyDefinedAndPopulated , DecimalFieldAlreadyDefinedAndPopulated ) ;



// Get data out of the maps like this....
string newStringValue = mIdToString.get(idField);
decimal newDecimalValue = mIdToDecimal.get(idField);

If you're just dealing with the two pieces of information - then that's probably okay.  But if you have more piences of information that needs to be kept for the Id, then you might want to look at a wrapper class to hold the info.  

 

All Answers

JeffreyStevensJeffreyStevens
There are a couple of things that you could look at.

Learn about Maps.  Maps are an orderd, collection of a key/value pair.  So you could create two maps, one to hold the id/string key/value pair, and one to hold the id/decimal key/value pair.  They would be defined like this...
// Define two maps
map<id,string> mIdToString = new map<id,string>();
mIdtoString.put(IdFieldAlreadyDefinedAndPopulated , StringFieldAlreadyDefinedAndPopulated) ;

map<id,decimal> mIdToDecimal = new map<id,decimal>();
mIdToDecimal.put(idFieldAlreadyDefinedAndPopulated , DecimalFieldAlreadyDefinedAndPopulated ) ;



// Get data out of the maps like this....
string newStringValue = mIdToString.get(idField);
decimal newDecimalValue = mIdToDecimal.get(idField);

If you're just dealing with the two pieces of information - then that's probably okay.  But if you have more piences of information that needs to be kept for the Id, then you might want to look at a wrapper class to hold the info.  

 
This was selected as the best answer
MarkInAtlantaMarkInAtlanta
thank you JefferyStevens..  a map was the 1st thing i tried to use, but as you anticipated, its more than just the 2 columns and quickly got pretty complicated..   I have it kind of working by borrowing an existing object type...     
JeffreyStevensJeffreyStevens
Yep - that would work also - I almost suggested that as another way.  So you could craete a map of the ID to any sObject that had a text field and a decimal field in it.  But that also get's complicated, as you'll need to instaniate the sObject - so you don't get attempt to de-refreence a null object. 
 
MarkInAtlantaMarkInAtlanta
after a bit of thought overnight, i've decided to go with a Map using a key and a delimited string. My concern over borrowing a sobject was with someone changing that object later and breaking what i did.

Little more work to handle the delimited string but i already have a set of utility methods that deconstruct/deconstruct a delimited string so all i need to add is a method to iterate over the map and return the array of fields.