Java 如何在不强制转换为整数的情况下将null转换为int?
好的,我试着用3个不同的条件得到奇数、偶数和负数Java 如何在不强制转换为整数的情况下将null转换为int?,java,arrays,casting,null,Java,Arrays,Casting,Null,好的,我试着用3个不同的条件得到奇数、偶数和负数 %2=0,%2=0,并且不能将null放入int[]中。必须使用整数[],或像-1这样的可分辨“标志值”将偶数和奇数作为整数[],而不是int[]。这是最简单的修复方法。一个int将只接受数字作为值 改为使用一个将容纳数字的整数,或者使用nullint是一种类型,只能容纳一个数字 您可以使用Integer[],它是一个包含原语编号的包装器对象。 从那时起,您可以使用对象数组,因此可以有一个空条目(空引用) 例如: Integer[] even;
%2=0,%2=0,并且不能将
null
放入int[]
中。必须使用整数[]
,或像-1
这样的可分辨“标志值”将偶数和奇数作为整数[]
,而不是int[]
。这是最简单的修复方法。一个int
将只接受数字作为值
改为使用一个将容纳数字的整数
,或者使用nullint
是一种类型,只能容纳一个数字
您可以使用Integer[]
,它是一个包含原语编号的包装器对象。
从那时起,您可以使用对象数组,因此可以有一个空条目(空引用)
例如:
Integer[] even;
您还可以指定一个特殊值来表示null
。示例:要编写一个接受数组并返回包含其元素子集的数组的方法,您需要做一些不同的事情。如果您可以使用ArrayList
,那将是最好的,因为它允许您在事先不知道大小的情况下创建数组。如果你不能,你将不得不通过两次。首先计算出结果中需要多少元素。然后,不必指定偶数[i]
,其中偶数
是要返回的数组,而是指定偶数[index]
,其中索引
是与循环索引不同的索引,只有在向循环索引中指定一个元素时才会递增1
public static int[] getEven(int[] array){
// Count the event elements in array, and assign `count` to the count
// (I'll let you fill in that part)
int[] even = new int[count];
int index = 0;
for(int i=0; i<array.length; i++){
if(array[i]%2 ==0){
even[index++] = array[i];
}
}
return even;
}
公共静态int[]get偶数(int[]array){
//对数组中的事件元素进行计数,并将'Count'分配给该计数
//(我让你填那部分)
int[]偶数=新的int[计数];
int指数=0;
对于(int i=0;i,这里的基本问题是,您预先声明了三个数组,而不知道它们需要保留多少值。当然,您知道它们将保留的最大值(10),但不知道最小值。这会影响对数组的调用。toString
使用列表
将是首选方法,但如果您的类中没有涉及铸造,那么我猜列表也没有涉及。为什么不让您的服务器返回一个字符串
(为了简单起见,我有意在这里省略StringBuilders,再次怀疑它们是否被涵盖)
主要内容:
import java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int[]数组=新的int[10];
System.out.print(“插入数组的10个值”);
对于(int i=0;iOkay,所以我尝试了你关于整数[]的建议,但现在它给了我这个…数组中的偶数是…[Ljava.lang.Integer;@3fe329eb数组中的奇数是…[Ljava.lang.Integer;@5ad52411数组中的负数是。。。[Ljava.lang.Integer;@5F3306AD这是因为数组是使用object-toString方法打印的。而不是System.out.println(偶数);
使用for循环并打印每个元素。尝试使用System.out.println(Arrays.toString(yourArray))
@javaOne:)+1.进一步信息:你真的需要数组中的洞吗?如果给我{3,2,8,1,6,5,9}
作为输入,我希望getEven
返回{2,8,6}
,而不是{2,8,6}
。如果你不需要这些孔,那么这将影响我们建议的解决方案。好吧,我尝试了你的想法,我得到了这个错误数组中的偶数是[I@efb78af
以下是我为(int i=0;i@AdamCalcanes您仍然不能在数组上使用System.out.println
。这看起来像是留在中的答案之一,但这是一个错误。请参阅上面的Java Devil评论。P.s.count=count*1
的目的是什么?我使用了count=count*1,因此如果条件没有发生,它只返回相同的结果计数的值,即如果数字不是偶数,计数器没有增加,那么它将保持为0,因为0*1=0,或者如果它是“3”,并且没有增加3*1=3,我想我可以留下一个空白,否则{}语句,但我只是急于回答你lol,如果我不能使用System.out.println
我该如何显示它呢?你可以使用for
循环来显示数组的值。for(int I=0;I
非常感谢,这是一个非常简单的修复方法,我不需要更改太多代码,并且基于我所学到的最新知识,这非常有效,因为你是对的,我对演员阵容和列表不太熟悉。
import java.util.Scanner;
public class Main {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
int [] array = new int[10];
System.out.print("Insert the 10 values of your array.");
for(int i=0; i<array.length; i++){
array[i] = input.nextInt();
}
int[] even = Server.getEven(array);
int[] odd = Server.getOdd(array);
int[] neg = Server.getNeg(array);
System.out.println("The even numbers in the array are...");
System.out.println(even);
System.out.println("The odd numbers in the array are...");
System.out.println(odd);
System.out.println("The negative numbers in the array are...");
System.out.println(neg);
input.close();
}
}
public class Server {
public static int[] getEven(int[] array){
int[] even = new int[array.length];
for(int i=0; i<array.length; i++){
if(array[i]%2 ==0){
even[i] = array[i];
}
else
{ even[i] = null;// <-- here it asks me to do (Integer) null;
}
}
return even;
}
public static int[] getOdd(int[] array){
int[] odd = new int[array.length];
for(int i=0; i<array.length; i++){
if(array[i]%2 !=0){
odd[i] = array[i];
}
else
{ odd[i] = null; // <-- here it asks me to do (Integer) null;
}
}
return odd;
}
public static int[] getNeg(int[] array){
int[] neg = new int[array.length];
for(int i=0; i<array.length; i++){
if(array[i]<0){
neg[i] = array[i];
}
else
{ neg[i] = null; // <-- here it asks me to do (Integer) null;
}
}
return neg;
}
}
Integer[] even;
public static int[] getEven(int[] array){
// Count the event elements in array, and assign `count` to the count
// (I'll let you fill in that part)
int[] even = new int[count];
int index = 0;
for(int i=0; i<array.length; i++){
if(array[i]%2 ==0){
even[index++] = array[i];
}
}
return even;
}
import java.util.Scanner;
public class Main {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
int [] array = new int[10];
System.out.print("Insert the 10 values of your array.");
for(int i=0; i<array.length; i++){
array[i] = input.nextInt();
}
System.out.println("The even numbers in the array are...");
System.out.println(Server.getEven(array));
System.out.println("The odd numbers in the array are...");
System.out.println(Server.getOdd(array));
System.out.println("The negative numbers in the array are...");
System.out.println(Server.getNeg(array));
input.close();
}
}
public class Server {
public static String getEven(int[] array){
String result = "";
for(int i=0; i<array.length; i++){
if(array[i]%2 ==0){
if(result.length() != 0)
{
result = result + ", ";
}
result = result + array[i];
}
}
return result;
}
public static String getOdd(int[] array){
String result = "";
for(int i=0; i<array.length; i++){
if(array[i]%2 !=0){
if(result.length() != 0)
{
result = result + ", ";
}
result = result + array[i];
}
}
return result;
}
public static String getNeg(int[] array){
String result = "";
for(int i=0; i<array.length; i++){
if(array[i]<0){
if(result.length() != 0)
{
result = result + ", ";
}
result = result + array[i];
}
}
return result;
}
}