0

I have a requirement to exclude last element from a list. For example if my list is like this:

List<String> str=new List<String>{'2023, 2024, 2025, 2026, 2027'}; 

I need a new list which will have only till 2026.

If I have a list :

List<String> = new List<String>{'2023, 2024'};

I need a new list which will have only 2023.

I created a new List<string> str1 = new List<string>();

I tried to use the traditional for loop but it seems to be not working.

for(Integer i=0; i< (str.size()-1); i++){
   str1.add(str(i));
}

Error: Method does not exist or incorrect signature: void str(Integer) from the type myApexClass

Any way to add the desired output to the new List str1, please suggest.

1
  • 1
    Again, please take more care to ensure that your example code is syntactically correct and that it's a faithful reproduction of what you actually have. List<String> str= '2023, 2024, 2025, 2026, 2027'; is not valid syntax.
    – Derek F
    Commented Jun 28, 2023 at 12:09

1 Answer 1

7

You could use .remove(index) to remove the last element from a list. The index of the last element is always size() - 1.

List<String> years = new List<String>{ '2023', '2024', '2025', '2026', '2027' };
years.remove(years.size() -1 );
System.debug(years); // Print (2023, 2024, 2025, 2026)

Please note that .remove(index) returns the elment being removed, so writing years = years.remove(years.size() -1 ); would result in the following error:

Illegal assignment from String to List

Once you call .remove(index) the size of the list changes.

The for-loop idea is fine, but you should use .get(index) or the square bracket notation to get the element of index i, that's why you got

Method does not exist or incorrect signature: void str(Integer)

List<String> years = new List<String>{ '2023', '2024', '2025', '2026', '2027' };
List<String> years2 = new List<String>();
Integer lastElementIndex = years.size() - 1;
for(Integer i=0; i<lastElementIndex; i++){
   years2.add(years.get(i)); // or years2.add(years[i]);
}
System.debug(years2);
4
  • Hi @rubenDG, I tried to use remove() but I'm getting this error: Illegal assignment from String to List<String>
    – sfdcDev
    Commented Jun 28, 2023 at 7:13
  • Here is how i'm trying to use it: List<String> allYearsList = new List<String>{ '2023', '2024', '2025', '2026', '2027' }; allYearsList = allYearsList.remove(allYearsList.size() -1 ); It is giving me error: Illegal assignment from String to List<String>
    – sfdcDev
    Commented Jun 28, 2023 at 7:16
  • 1
    @sfdcDev remove(index) returns the element that has been removed from the list, that's why you got that error. You don't need to reassing the result to allYearsList. Please change allYearsList = allYearsList.remove(allYearsList.size() -1 ); to allYearsList.remove(allYearsList.size() -1 );. By the way, you could try my snippet in the developer console
    – RubenDG
    Commented Jun 28, 2023 at 7:19
  • I did try the other way suggested by you: Integer lastElementIndex = years.size() - 1; This is working as expected for me, Thanks for your time and effort on this @RubenDG
    – sfdcDev
    Commented Jun 28, 2023 at 7:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .