1

I would like to know if it is possible to go through the list and create the object, enter the values of the list elements in the constructor. For example:

String result = "1 2 3 4 5 6";
String list[]=result1.split(""); // create a list of String
//next i want to do the following
BigInteger a1 = new BigInteger (l1);// String l1 = "1";String l2 = "2";
BigInteger a2= new BigInteger (l2);
BigInteger result = a1.add(a2);
etc

Is it possible to do that using for each loop? How it should look like? Thanks

bhxbr922
  • 47
  • 5
  • I'm just a beginner, so please do not answer with advanced exsamples) – bhxbr922 Jul 02 '20 at 11:23
  • Is this answering your question? https://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java – Maciej Piorun Jul 02 '20 at 11:25
  • Yes, if you want to sum all values just start with `BigInteger result = BigInteger.ZERO;` and in loop add new BigInteger representing currently value loop is handling. Just split on `" "` (space) not `""` – Pshemo Jul 02 '20 at 11:25

5 Answers5

1

Indeed, you could use a loop to go over the split array and accumulate the results in a list:

List<BigInteger> results = new ArrayList<>(list.length);
for (str : list) {
    results.add(new BigInteger(str));
}

You could also use a stream, which would arguably be more elegant, although it's matter taste:

List<BigInteger> result = 
    Arrays.stream(list)
          .map(BigInteger::new)
          .collect(Collectors.toList());
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1
    String result = "1 2 3 4 5 6";
    String list[]=result.split(" "); 
    // You should split using space " " to create this list: {"1","2","3","4","5","6"}
    
    BigInteger [] objects = new BigInteger[list.length];
    BigInteger sum = new BigInteger("0"); // initialize sum with Zero
    for(int i = 0 ; i < objects.length ; i++){
        objects[i] = new BigInteger(list[i]);
        sum = sum.add( objects[i] ); // Add the value of each BigInteger to sum
    }
0

You can have one BigInteger outside the loop and inside do:

    BigInteger sum = BigInteger.ZERO;
    
    String result = "1 2 3 4 5 6";
    String list[] = result.split(" ");
    for (int i = 0; i < list.length; i++) {
        sum.add(BigInteger.valueOf(Integer.parseInt(list[i])));
    }
tdranv
  • 1,140
  • 11
  • 38
0

Yes it's possible, One way to do this is

    String result = "1 2 3 4 5 6";
    String list[]=result.split(" "); // create a list of String

    //Create A Array of objects

    BigInteger[] objects=new BigInteger[list.length];
    BigInteger sum = new BigInteger("0"); // initializing with 0

    for(int i=0;i<list.length;i++)
    {
        objects[i]=new BigInteger(list[i]);
        sum=sum.add(objects[i]);// Assign each objects

    }
    System.out.println("Sum : "+sum); // print sum
AgentP
  • 6,261
  • 2
  • 31
  • 52
  • great! The only thing for understanding left: how can we now get sum of each BigInteger from the list? As I know we only can use "add" – bhxbr922 Jul 02 '20 at 11:34
  • @bhxbr922 I have edited my answer check it ... And also check at other answers too ... As there are several ways to do it ... – AgentP Jul 02 '20 at 12:22
0

Yes it is. Functionally like this:

List<BigInteger> integers = Arrays.stream(list).map(x -> new BigInteger(x)).ToList();

if you want to sum:

BigInteger sum = Arrays.stream(list).map(x -> new BigInteger(x)).reduce(0, (a, b) -> a+b);

Same can be done with for loops but with more code