0

In this program, I want each list to have corresponding values. For example, spot 0 in each list should have attached values.

The output I want is to remove the identical values from 'value' and get the sum of the numbers for every corresponding 'valueA' and 'valueB' for the initial 'value' letter.

Ex)

a-

valueA sum = 14.4

valueB sum = 19.9

b-

valueA sum = 16.8

valueB sum = 10.3

Would this work better if the list was multi-dimensional? Sorry if this is a bad question, but I am struggling to accomplish this.

import java.util.ArrayList;
import java.util.List;

public class codehelp {
    public static List<String> value = new ArrayList<>();
    public static List<Double> valueA = new ArrayList<>();
    public static List<Double> valueB = new ArrayList<>();


    public static void main(String[] args) {
        value.add("a"); //add some values to the first list
        value.add("a");
        value.add("b");
        value.add("b");

        valueA.add(5.3); //add some values to the second list
        valueA.add(9.1);
        valueA.add(15.3);
        valueA.add(1.5);

        valueB.add(19.3); //add some values to the third list
        valueB.add(.6);
        valueB.add(6.3);
        valueB.add(4.0);

        //code here

    }
}

Joshua Varghese
  • 5,082
  • 1
  • 13
  • 34
  • Java is object-oriented, and you should use that functionality rather than using multiple separate lists. I'd suggest making some type of class that can store your string and two doubles so that you can just find each object with the corresponding string value and sum those up. – Mihir Kekkar Apr 26 '20 at 07:23
  • Where does the input values come from? What is the actual goal you're trying to reach? Can you refactor the code to call something like `add("a", 5.3)` and so on instead of having separate lists? – Bodo Thiesen Apr 26 '20 at 07:27
  • You can use HashMap it would be easiest way. – ajayg2808 Apr 26 '20 at 07:27
  • According to your example, `value` contains four elements in the following order: a, a, b, b. What happens if the order is: a, b, a, b ? And what happens if list `valueA` contains only one value and not four? In those situations, what output do you want to get? – Abra Apr 26 '20 at 07:31
  • I should have been more clear. I wrote this code snippet to simplify asking. The actual values are being taken from an excel document and inputted into the arrays (using apache poi). I have done that, but I am still having trouble with the same problem. – Astro Clark Apr 26 '20 at 07:41
  • To respond to Abra, 'valueA' and 'valueB' will always contain the same amount of values a 'value'. However, it should not matter how many or which order the 'value' values are in. – Astro Clark Apr 26 '20 at 07:48

0 Answers0