我如何调用返回int数组的函数 [英] How do I can call function that returns an int array

查看:102
本文介绍了我如何调用返回int数组的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>我正在使用下面的代码为返回数组中的值的函数提供值但是当我调用该函数并看到出现错误的代码时我收到错误

< 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 等于最大索引,因此 j 将等于 ta.length ,并且获得 ta [j] 将尝试超越阵列。



-SA

>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 the ta 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 this NullPointerException thing. This means that there wasn't anything to work on, null makes sense? Read about this exception here[^]


In addition to Solution 1: in j = i + 1, j would go out of the range of array indices when i is equal to the maximum index, so j will be equal to ta.length, and getting ta[j] will be an attempt to point beyond the array.

—SA


这篇关于我如何调用返回int数组的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆