我想将 Vararg Integer 转换为 Vararg int?
I want to convert Vararg Integer to Vararg int?
下面的代码在我的可变参数方法中使用整数值时工作正常。但我想使用“int”而不是“Integer”。
代码如下:
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);
}
}
我想在我的方法中使用“int....variable”而不是“Integer...variable”。我假设我需要将变量“intarray”转换为 int。
请帮助我卡在那个部分,谢谢。
您可以使用带自动装箱功能的 for
循环从您的列表中创建一个 int 数组。然后你可以改变所有的方法签名。
int[] intarray = new int[lnumber.size()];
for(int i = 0; i < intarray.length; i++)
intarray[i] = lnumber.get(i);
您也可以使用流。
int[] intarray = lnumber.stream().mapToInt(Integer::intValue).toArray();
您需要执行以下步骤将您的程序从 Integer 更改为 int...
将DisplayNumbers(Integer...a)
的参数更改为DisplayNumbers(int...a)
再次将SumOfNumbers(Integer...a)
的参数更改为SumOfNumbers(int...a)
之后,您需要在main
方法中做一些代码更改。即代替
Integer[] intarray = new Integer[lnumber.size()];
intarray = lnumber.toArray(intarray);
你需要写
int[] intarray = lnumber.stream().mapToInt(i -> i).toArray();
请查找完整程序:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MediumLevelExcercisesA {
Scanner number;
Scanner yorn;
public MediumLevelExcercisesA(){
}
static void DisplayNumbers(int[] a) {
for (int b = 0; b < a.length; b++) {
System.out.print(a[b]);
System.out.print(" ");
}
}
static void SumOfNumbers(int...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);
//Remove below two commented line because it is not need further
//Integer[] intarray = new Integer[lnumber.size()];
//intarray = lnumber.toArray(intarray);
// This can be used for if not java 8 compatible
/*int[] intarray = new int[lnumber.size()];
Integer[] temp = lnumber.toArray(new Integer[lnumber.size()]);
for (int n = 0; n < lnumber.size(); ++n) {
intarray[n] = temp[n];
}*/
//Java 8 and above
int[] intarray = lnumber.stream().mapToInt(i -> i).toArray();
MediumLevelExcercisesA.DisplayNumbers(intarray);
MediumLevelExcercisesA.SumOfNumbers(intarray);
}
}
下面的代码在我的可变参数方法中使用整数值时工作正常。但我想使用“int”而不是“Integer”。
代码如下:
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);
}
}
我想在我的方法中使用“int....variable”而不是“Integer...variable”。我假设我需要将变量“intarray”转换为 int。
请帮助我卡在那个部分,谢谢。
您可以使用带自动装箱功能的 for
循环从您的列表中创建一个 int 数组。然后你可以改变所有的方法签名。
int[] intarray = new int[lnumber.size()];
for(int i = 0; i < intarray.length; i++)
intarray[i] = lnumber.get(i);
您也可以使用流。
int[] intarray = lnumber.stream().mapToInt(Integer::intValue).toArray();
您需要执行以下步骤将您的程序从 Integer 更改为 int...
将
DisplayNumbers(Integer...a)
的参数更改为DisplayNumbers(int...a)
再次将
SumOfNumbers(Integer...a)
的参数更改为SumOfNumbers(int...a)
之后,您需要在
main
方法中做一些代码更改。即代替Integer[] intarray = new Integer[lnumber.size()]; intarray = lnumber.toArray(intarray);
你需要写
int[] intarray = lnumber.stream().mapToInt(i -> i).toArray();
请查找完整程序:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MediumLevelExcercisesA {
Scanner number;
Scanner yorn;
public MediumLevelExcercisesA(){
}
static void DisplayNumbers(int[] a) {
for (int b = 0; b < a.length; b++) {
System.out.print(a[b]);
System.out.print(" ");
}
}
static void SumOfNumbers(int...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);
//Remove below two commented line because it is not need further
//Integer[] intarray = new Integer[lnumber.size()];
//intarray = lnumber.toArray(intarray);
// This can be used for if not java 8 compatible
/*int[] intarray = new int[lnumber.size()];
Integer[] temp = lnumber.toArray(new Integer[lnumber.size()]);
for (int n = 0; n < lnumber.size(); ++n) {
intarray[n] = temp[n];
}*/
//Java 8 and above
int[] intarray = lnumber.stream().mapToInt(i -> i).toArray();
MediumLevelExcercisesA.DisplayNumbers(intarray);
MediumLevelExcercisesA.SumOfNumbers(intarray);
}
}