1

i have some int values stored in ArrayList1 (imagine a table with one column). I need to create another ArrayList2 (second column), which will have values, that are the max values of a range in ArrayList1, for example last 3 values. for example:

1 null
2 null
1 2
3 3
2 3
1 3

So the secondt column - ArrayList2, will contain for each row max value of the last 3 corresponding rows in ArrayList1. (same as in xls, B3=MAX(A1:A3)...). I can do it by creating for each row with a loop that goes and finds out max value, or i can loop and create subArrayList and use collections.max, but both these solutions require a loop for every row, which isn't very practical, isnt there a simpler way? something like arrayList.max(1,3) to get the number straight away?

5
  • is the range (last 3 value) constant? Commented Jan 8, 2016 at 13:16
  • @sanbhat I believe OP is looking for a method in cases where the lists are longer and the last maximum might not be constant Commented Jan 8, 2016 at 13:16
  • I am looking to be number 3 flexible, if i need it to be 200, in a 100000 long arraylist for example, simply pit something as that xls function i mentioned Commented Jan 8, 2016 at 13:20
  • i have one question here: what should happen for second row in arraylist2? why is it null when we have max value 3 from {2,1,3} of first array list Commented Jan 8, 2016 at 13:37
  • It goes the other way, sorry not to mention, it ahould look for previous 3, as in row 2 there arent previous 3 to look for, so it starts at row 3, and looks at rows 1-3, same principle if i need max from 100elements, i can get a 1st value in 100th row Commented Jan 8, 2016 at 13:52

3 Answers 3

2

You can do something like the code I show below. You iterate the first array, calculating the maximum and updating the second array accordingly. Note that you need an extra array to store the intervals.

private void m1(List<Integer> array1, List<Integer> array2, int range) {

    Integer[] rangeArray = new Integer[range];

    //Iterate first array

    for(int i = 0; i < array1.size(); i++) {

        Integer x = array1.get(i);

        rangeArray[i%range] = x;

        //Update second array

        if(i < range - 1) {
            array2.add(null);
        }
        else {
            int max = Collections.max(Arrays.asList(rangeArray));
            array2.add(max);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer:), sometimes i really get blocked and don't see the obvious solution, mainly cause i've done basically the same for moving average yesterday:P
I just updated (slightly) the algorithm because I misunderstood that you only want to calculate the maximum of sublists of N elements (being N the range)
Also note that the algorithm works just fine but it would be more efficient if "rangeArray" was represented as a max-heap (instead of a static array).
1

Using Collections.max

    int startFrom = 2; // configurable number
    List<Integer> intList = Arrays.asList(1,2,1,3,2,1,4);
    List<Integer> sortedList =  new ArrayList<>();
    for (int i = 0; i < intList.size(); i++) {
        if(i < startFrom){
            sortedList.add(null);
            continue;
        }
        ArrayList<Integer> list = new ArrayList<Integer>(intList.subList(i -startFrom, i+1));
        sortedList.add(Collections.max(list));
    }
    System.out.println(sortedList);

Output is :[null, null, 2, 3, 3, 3, 4]

Comments

0

Ok. So your size of list to compare can vary , in that case build an array list out of the numbers to compare say List<?> list = Arrays.asList( numbersToCompare ); then Collections.max( list ), the list should contain objects that can be compared in other words needs to be comparable. ( If not write your own comparator )

1 Comment

Thanks, but might be problematic to get maximum from 200 elements, and i need the element number to be easily changable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.