Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从私有方法调用数组?_Java_Arrays_Methods_Call_Private - Fatal编程技术网

Java 如何从私有方法调用数组?

Java 如何从私有方法调用数组?,java,arrays,methods,call,private,Java,Arrays,Methods,Call,Private,所以,我必须制作一个能够产生结果的应用程序,我想我已经完成了所有的事情????但是我不知道如何让我的私有方法outputData从私有方法getValue输出数组结果 这就是我所拥有的 // CreateResults.java // Create poll results and output them to a file. import java.io.FileNotFoundException; import java.util.Formatter; import java.util.F

所以,我必须制作一个能够产生结果的应用程序,我想我已经完成了所有的事情????但是我不知道如何让我的私有方法outputData从私有方法getValue输出数组结果

这就是我所拥有的

 // CreateResults.java
// Create poll results and output them to a file.
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.IllegalFormatException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class CreateResults
{
   private int getValue()
   {
         int[]results = { 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 };
         outputData(results);

   } // end method getValue

   private void outputData(int[] output) 
   {

         for (int row = 0; row < results.length; row++) {
         System.out.println(input [row]);
   }//end for
   } // end method outputData

   public static void main( String args[] )
   {
      CreateResults application = new CreateResults();
      application.outputData();
   } // end main
} // end class CreateResults
//CreateResults.java
//创建投票结果并将其输出到文件。
导入java.io.FileNotFoundException;
导入java.util.Formatter;
导入java.util.FormatterClosedException;
导入java.util.IllegalFormatException;
导入java.util.NoSuchElementException;
导入java.util.Scanner;
公共类CreateResults
{
私有int getValue()
{
int[]结果={1,1,1,1,2,3,3,4,5,6,7,8,9};
输出数据(结果);
}//结束方法getValue
私有void outputData(int[]输出)
{
for(int row=0;row
您的数组仅存在于方法范围内。将定义置于外部,使其成为类变量,如下所示:

public class CreateResults
{
int[]results;
   private int getValue()
   {
         results = new int[] { 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 };
         outputData(results);

   } // end method getValue
}

要使代码正常工作,您需要将
getValue
设置为public(由于它不返回任何内容,所以将其设置为void)

然后在
main
中调用
application.getValue()
,它将创建数组,然后调用
outputData

public void getValue()
{
     int[]results = { 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9 };
     outputData(results);

} // end method getValue

public static void main( String args[] )
{
  CreateResults application = new CreateResults();
  application.getValue ();
} // 
另外,当您
outputData
正在处理
output
的输入参数时,您需要将其更改为

 private void outputData(int[] output) 
 {

     for (int row = 0; row < output.length; row++) {
         System.out.println(output[row]);
     }
 }//e
private void outputData(int[]输出)
{
for(int row=0;row
你能帮我解释一下这到底是什么意思吗。。对不起,我是初学者!当我这样做时,{}括号给了我一个非法的表达式错误开始。行结果=(int[]){1,1,1,1,2,2,3,3,4,5,6,7,8,9};由于括号的原因,这一行出现了很多错误,我很难理解对不起。正确的语法应该是:result=newint[]{1,2,3};好的,我明白你的意思,并且解决了我三个错误中的两个。最后,行(int row=0;row