我如何调用返回int数组的函数 [英] How do I can call function that returns an int array
问题描述
>我正在使用下面的代码为返回数组中的值的函数提供值但是当我调用该函数并看到出现错误的代码时我收到错误
< pre lang =java> public class Tech
{
public static int [] getMax( int input1, int [] input2)
{
int temp = 0;
int count = 0,min = 0;
Integer [] ta = new Integer [input2.length];
for ( int i = 0; i< ta.length; i ++)
{
for ( int j = i + 1; j< ta.length; j ++ )
{
if (ta [j]< ta [i]) // 错误:线程main中的异常java.lang.NullPointerException
{
temp = ta [j];
ta [j] = ta [i];
ta [i] = temp;
}
}
}
int [] a1 = new int [ta.length];
for ( int k = 0; k< ta.length; k ++)
{
min = min + ta [k];
if (min> input1)
{
break ;
}
a1 [k] = ta [k];
count = count + 1;
}
count = a1.length-count;
a1 = Arrays.copyOf(a1,a1.length-count);
return a1;
}
public static void main( String [] args)
{
int rs = 50;
int t1 [] = { 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 6 , 7 , 4 } ;
int k [] = getMax(rs,t1); // 错误:线程main中的异常java.lang.NullPointerException
System.out.print(k.length);
}
}
这是因为你的代码块为
if (ta [j]< ta [i])>
执行ta
数组变量为null(其中不包含任何元素)。您只需设置数组的大小,但尚未添加任何元素。
为什么不简单地将数组传递给它?
// 将数组传递给变量
int [] ta = input2;
从Java学习Java中的数组文档 [< a href =http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.htmltarget =_ blanktitle =New Window> ^ ]。其次,你需要理解这个NullPointerException
的事情。这意味着没有任何工作,null
有意义吗?请阅读此处 [ ^ ]
< blockquote>除了解决方案1:在 j = i + 1时,j
在 i $ c时将超出数组索引的范围$ c>等于最大索引,因此
j
将等于 ta.length
,并且获得 ta [j]
将尝试超越阵列。
>I am using below code to provide value to the funtion that return values in array but i am getting an error when i am calling that function and see the code where the error arise
public class Tech
{
public static int[] getMax(int input1,int[] input2)
{
int temp=0;
int count=0,min=0;
Integer[] ta=new Integer[input2.length];
for(int i=0;i<ta.length;i++)
{
for( int j=i+1;j<ta.length;j++)
{
if(ta[j]<ta[i])//error :Exception in thread "main" java.lang.NullPointerException
{
temp=ta[j];
ta[j]=ta[i];
ta[i]=temp;
}
}
}
int[] a1=new int [ta.length];
for(int k=0;k<ta.length;k++)
{
min=min+ta[k];
if(min>input1)
{
break;
}
a1[k]=ta[k];
count=count+1;
}
count=a1.length-count;
a1=Arrays.copyOf(a1, a1.length-count);
return a1;
}
public static void main(String[] args)
{
int rs=50;
int t1[]={1,12,5,111,200,1000,10,9,6,7,4};
int k[] = getMax(rs,t1);//error :Exception in thread "main" java.lang.NullPointerException
System.out.print(k.length);
}
}
That is because when your code block of
if(ta[j]<ta[i])>
is executed theta
array variable is null (contains no element in it). You've just set the size of the array, but have not added any element to it.
Why don't you simply pass the array to it?
// pass the array down to the variable int[] ta = input2;
Learn Arrays in Java from Java documentations[^]. Secondly you need to understand thisNullPointerException
thing. This means that there wasn't anything to work on,null
makes sense? Read about this exception here[^]
In addition to Solution 1: inj = i + 1, j
would go out of the range of array indices wheni
is equal to the maximum index, soj
will be equal tota.length
, and gettingta[j]
will be an attempt to point beyond the array.
—SA
这篇关于我如何调用返回int数组的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!