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
mobile vistexmobile vistex 

for each loop

hi i want two compare two values in a loop for ex my code is
for(String sr : uniqueData){
        list<String> dr = sr.split('~',2); 
        agm = dr[0];
        dvr = dr[1];
}

here in my uniqueData there are 5 Strings . when ever i enter in to loop iam spliting the String and assigning it to two variables. now my question is if two strings in agm ae same i dont want to print it for ex{if i get teja in first iterator. and also teja in second iterator . i dont want that name }.  how can i compare first agm and second agm
 
Best Answer chosen by mobile vistex
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

Please try below code.
List<List<String>> dr;
int i=0;
for(String sr : uniqueData){
        //list<String> dr = sr.split('~',2); 
		dr.add(sr.split('~',2));
		if(i>0){
			String agmPrevious = dr[i-1];
			String dvrPrevious = dr[i-1];
			
			String agmCurrent = dr[i];
			String dvrCurrent= dr[i];
			
			//if(agmPrevious ==agmCurrent){
				//YOUR LOGIC
			}
		}
		i++;
        
}

Let us know if it helps you.

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

Please try below code.
List<List<String>> dr;
int i=0;
for(String sr : uniqueData){
        //list<String> dr = sr.split('~',2); 
		dr.add(sr.split('~',2));
		if(i>0){
			String agmPrevious = dr[i-1];
			String dvrPrevious = dr[i-1];
			
			String agmCurrent = dr[i];
			String dvrCurrent= dr[i];
			
			//if(agmPrevious ==agmCurrent){
				//YOUR LOGIC
			}
		}
		i++;
        
}

Let us know if it helps you.
This was selected as the best answer
Dhriti MoulickDhriti Moulick
Hi,

   If you are using uniqueData as Set<String> it won't accept duplicate records.So no need to compare it.If you are using List<String> you need to compare.


Thanks,
Dhriti