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
PS81PS81 

two Dimensional list - string

Hi

i have a two dimensinal array populated but need help on how to iterate thru it:

List<List<string>> Wdays = new List<list<string>>();
List<String> Mnth = new List<String>();    

Mnth.add('JAN'); Mnth.add(string.valueof(jan));
Wdays.add(mnth);

I wish to know how to loop thru the two dimensional list to display the values...any help please?
Best Answer chosen by PS81
Miroslav SmukovMiroslav Smukov
Finally, i think this might be the solution you are looking for regarding how to populate the 'Wdays' variable with number of days for each month:
 
List<List<string>> Wdays = new List<List<string>>();
Wdays.add(new List<string>{'Jan', String.valueof(Date.daysInMonth(2015, 1))}); 
Wdays.add(new List<string>{'Feb', String.valueof(Date.daysInMonth(2015, 2))});
Wdays.add(new List<string>{'Mar', String.valueof(Date.daysInMonth(2015, 3))});

You can then obtain the value for each month in this way:
String daysInMonth = Wdays[monthNumber][0];
//Where monthNumber is starting from 0 -- 0 = January, 1 = February, 2 = March, etc.



 

All Answers

Miroslav SmukovMiroslav Smukov
List<List<string>> Wdays = new List<List<string>>();

Wdays.add(new List<String>{'Monday', 'Tuesday', 'Wednesday'});
Wdays.add(new List<String>{'Thrusday', 'Friday', 'Saturday'});
Wdays.add(new List<String>{'Sunday'});

//this is how you iterate through two dimensional array.
for(Integer i=0;i < Wdays.size();i++)
    for(Integer j=0;j < Wdays[i].size();j++)
          {
              String val = Wdays[i][j];
              System.debug(val);                
          }

Does this help?

The two FOR loops is what you are looking for I believe. The first 4 lines are just added for context and for testing pupose.
PS81PS81
thkx miroslav, 

i'm not sure if i'm using the array of list in the right way.....all i wanted is

wdays ={(Jan,31),(feb,28)....)

with the below code i think i'm not getting what i need:

        mnth.add('JAN'); Mnth.add(string.valueof(jan)); Wdays.add(mnth);
        mnth.add('FEB'); Mnth.add(string.valueof(feb)); Wdays.add(mnth);
        mnth.add('MAR'); Mnth.add(string.valueof(mar)); Wdays.add(mnth);

 
PS81PS81
seems mnth is getting amended with the previous data I.E. the first line amend JAN , 30 to WDAYS but the second line doesn JAN,30 and FEB, 28 and so on....if i make mnth clear it's not working....any inputs on this please?

 
Miroslav SmukovMiroslav Smukov
Does the code below give you the expected result?
 
List<List<string>> Wdays = new List<List<string>>();
Wdays.add(new List<string>{'Jan','31'}); 
Wdays.add(new List<string>{'Feb', '28'});
Miroslav SmukovMiroslav Smukov
In your code sample:
 
mnth.add('JAN'); Mnth.add(string.valueof(jan)); Wdays.add(mnth);
mnth.add('FEB'); Mnth.add(string.valueof(feb)); Wdays.add(mnth);
mnth.add('MAR'); Mnth.add(string.valueof(mar)); Wdays.add(mnth);

You are always adding the reference to the same object when you call "Wdays.add(mnth);" so you end up with one object being added 3 times to Wdays array and it has values for 'JAN', 'FEB' and 'MAR' added to it.
Miroslav SmukovMiroslav Smukov
Finally, i think this might be the solution you are looking for regarding how to populate the 'Wdays' variable with number of days for each month:
 
List<List<string>> Wdays = new List<List<string>>();
Wdays.add(new List<string>{'Jan', String.valueof(Date.daysInMonth(2015, 1))}); 
Wdays.add(new List<string>{'Feb', String.valueof(Date.daysInMonth(2015, 2))});
Wdays.add(new List<string>{'Mar', String.valueof(Date.daysInMonth(2015, 3))});

You can then obtain the value for each month in this way:
String daysInMonth = Wdays[monthNumber][0];
//Where monthNumber is starting from 0 -- 0 = January, 1 = February, 2 = March, etc.



 
This was selected as the best answer
PS81PS81
how can i handle this in such a way that Wdays[0] has Jan,30 and Wdays[1] has Feb,28 etc? any help on it please?
PS81PS81
the number of days in month are calculated with reference to holidays etc and hence cannot use 

Wdays.add(new List<string>{'Jan', String.valueof(Date.daysInMonth(2015, 1))});
PS81PS81
thanks miroslav , it works as i needed it
Miroslav SmukovMiroslav Smukov
Does this work?
 
List<string> Wdays = new List<string>();
Wdays.add('Jan,' + jan); 
Wdays.add('Feb,' + feb);
Wdays.add('Mar,' + mar);

Where jan, feb, mar are Integer variables with your calculated number of days.
PS81PS81
yes it works too but having it in two dimensional list gives me flexibility in validations
Miroslav SmukovMiroslav Smukov
I'm glad I helped! :)
PS81PS81
yes, much appreciate ur help here....thkx again