Home > front end >  Value changed in ArrayList outside of for loop
Value changed in ArrayList outside of for loop

Time:05-25

I am learning Java and wanted to find the contiguous sub-array with maximum sum. I am able to find it, but when I am going outside the for loop, the value of the sub-array saved in an ArrayList changes.

public class FindingSumOfContiguousSubArray {
    
    //code for computing sum of an array list
    public int getSum (ArrayList <Integer> arraylist ) {
        
        int sum = 0;
        for (int i=0; i<arraylist.size(); i  ) {
            sum   = arraylist.get(i);
        }
        return sum;
    }
    
    private ArrayList<Integer> contigiousSubArray(int [] array){

        ArrayList <Integer> finalList = new ArrayList<Integer>();
        int n = array.length;
        int minVal = -1000;
        for(int i = 0; i<n; i  ) {
            ArrayList<Integer> aList = new ArrayList<Integer>(); //taking local object of ArrayList
            for(int j = i; j<n; j  ) {
                aList.add(array[j]); //{-2, 1}
                
                int sum = getSum(aList);
                
                //System.out.println(minVal);
                if (sum > minVal) {
                    minVal = sum;
                    //finalList.clear();
                    finalList = aList;
                    System.out.println(sum);
                    System.out.println(finalList);  
                }
                else continue;
            }   
        }
        
        System.out.println(finalList);
        return finalList;
    }

    public static void main(String[] args) {
        FindingSumOfContiguousSubArray cSA = new FindingSumOfContiguousSubArray(); //creating class object
    
        int [] inpArray = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
        
        ArrayList <Integer> contFinalList = cSA.contigiousSubArray(inpArray);
        
        //System.out.println(contFinalList);
        //System.out.println(cSA.getSum(contFinalList));
    }

}

This code gives output:

[1, -3, 4, -1, 2, 1]

5

[4, -1, 2]

6

[4, -1, 2, 1]

[4, -1, 2, 1, -5, 4]

I am not sure why my arraylist is showing [4, -1, 2, 1, -5, 4] outside the for loop.

CodePudding user response:

I'm not exactly sure what you're looking for here, but maybe try this (you don't need your summing method this way):

private static List<Integer> contigiousSubArray(int [] array){

    List<Integer> finalList = new ArrayList<>();
    int minVal = Integer.MIN_VALUE;
    for(int i = 0; i < array.length; i  ) {
        List<Integer> aList = new ArrayList<Integer>(); //taking local object of ArrayList
        for(int j = i; j < array.length; j  ) {
            aList.add(array[j]); //{-2, 1}
            
            int sum = aList.stream()
                           .collect(Collectors.summingInt(Integer::intValue));
            
            //System.out.println(minVal);
            if (sum > minVal) {
                minVal = sum;
                finalList.clear();
                finalList.addAll(aList);  
            }
        }   
    }
    
    return finalList;
}

CodePudding user response:

try this :

  public int contigiousSubArray(int[] arr) {
  int[] result = new int[arr.length];
  result[0]=arr[0];
  for (int i = 1; i < arr.length; i  ) {
   result[i]=Math.max(result[i-1] arr[i], arr[i]);
  }
   int maxSumArray = result[0];
         for (int j = 1; j              if(maxSumArray<result[j])
              maxSumArray = result[j];
         }
 
  return maxSumArray;
 }```
  • Related