[SalesForce] apex add list to list

I have this code:

String cantidad;
String total;

List<List<String>> pList = new List<List<String>>();
List<String> lstStr = new List<String>();
List<QuoteLineItem> acList = [SELECT Quantity, total__c FROM QuoteLineItem 
WHERE QuoteId = '0Q01D00'];

    for(QuoteLineItem ac : acList)
    {
        cantidad = string.valueOf(ac.Quantity); 
        total = string.valueOf(ac.total__c);
        lstStr.add(cantidad);
        lstStr.add(total);
    }
    pList.add(lstStr);
System.debug(pList);

the output is:

((2.00, 10.00, 1.00, 10.00, 1.00, 10.00, 2.00, 44.00))

Im trying to separate each 2 values. it should show this:

((2.00, 10.00), (1.00, 10.00), (1.00, 10.00), (2.00, 44.00))

So i can access them like pList[0], pList[1], pList[2], pList[3]

Best Answer

The problem is the instance of the list you are working with is constantly being built with new values. To solve your issue, you could use the following

List<List<String>> pList = new List<List<String>>();

for (QuoteLineItem ac : [SELECT Quantity, total__c FROM QuoteLineItem WHERE QuoteId = '0Q01D00']) {
    pList.add(new List<String>{String.valueOf(ac.Quantity), String.valueOf(ac.total__c)});
}
System.debug(pList[0]);
System.debug(pList[1]); 

Note, the for loop has become a soql for loop. You may also consider the use of a Map to related collection data but note, the get() method does not support indexing.