Search code examples
javavariadic-functionsvariable-length-array

I want to convert Vararg Integer to Vararg int?


The below code is working fine using Integer values for my variadic method. But I want to use "int" instead of "Integer".

Here is the code:

package JavaPracticeShuffler;

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


public class MediumLevelExcercisesA {
    
    Scanner number;
    Scanner yorn;
    
    public MediumLevelExcercisesA(){
        
    }
    
    static void DisplayNumbers(Integer...a) {
        for (int b = 0; b < a.length; b++) {
            System.out.print(a[b]);
            System.out.print(" ");
        }
    }
    
    static void SumOfNumbers(Integer...a) {
        int total = 0;
        for (int b = 0; b < a.length; b++) {
            total += a[b];
        }
        System.out.println(" ");
        System.out.println("Sum: " + total);
    }
    
    
        
    public static void main(String[] args) {
        MediumLevelExcercisesA obj01 = new MediumLevelExcercisesA();
        List<Integer> lnumber = new ArrayList<Integer>();
                
        boolean numbercounter = true;
        
        while (numbercounter) {
            obj01.number = new Scanner(System.in);
            System.out.println("Please input a number");
            int inputnumber = obj01.number.nextInt();
            lnumber.add(inputnumber);
            
            System.out.println("Number/s you've entered so far are the following: " + lnumber);
            
            obj01.yorn = new Scanner(System.in);
            System.out.println("Do you want to continue?");
            String iyorn = obj01.yorn.nextLine().toUpperCase();
            
            if (iyorn.equals("YES")||iyorn.equals("Y")) {
                System.out.println("You've answered yes, program will proceed.");
            }
            else if (iyorn.equals("NO")||iyorn.equals("N")) {
                System.out.println("You've answered no, program ends.");
                numbercounter = false;
                break;
            }
            else {
                System.out.println("Answer not understood, program continues.");
            }
        } //end of while loop.
        
        System.out.println("Final review of numbers entered: " + lnumber);
        
        Integer[] intarray = new Integer[lnumber.size()];
        intarray = lnumber.toArray(intarray);
        MediumLevelExcercisesA.DisplayNumbers(intarray);
        MediumLevelExcercisesA.SumOfNumbers(intarray);
    }
    
}


I want to use "int....variable" instead of "Integer...variable" on my methods. I am assuming that I need to convert variable "intarray" to an int.

Please help I'm stuck on that part, thanks.


Solution

  • You can create a int array from your List by utilizing a for loop with autoboxing. Then you can change all the method signatures.

    int[] intarray = new int[lnumber.size()];
    for(int i = 0; i < intarray.length; i++)
         intarray[i] = lnumber.get(i);
    

    You can also use Streams.

    int[] intarray = lnumber.stream().mapToInt(Integer::intValue).toArray();