Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.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 如何将值从方法中填充的数组返回到main中的不规则数组_Java_Arrays_Jagged Arrays - Fatal编程技术网

Java 如何将值从方法中填充的数组返回到main中的不规则数组

Java 如何将值从方法中填充的数组返回到main中的不规则数组,java,arrays,jagged-arrays,Java,Arrays,Jagged Arrays,我有一个主要方法,它创建一个包含3行和随机数列的不规则数组。然后将数组传递给名为Proj09Runner的类,以使用从左到右、从上到下的整数值填充数组元素,从1开始,以行的长度总和结束 import java.util.Random; import java.util.Date; class Proj09{ public static void main(String[] args){ //Create a pseudo-random number generator

我有一个主要方法,它创建一个包含3行和随机数列的不规则数组。然后将数组传递给名为Proj09Runner的类,以使用从左到右、从上到下的整数值填充数组元素,从1开始,以行的长度总和结束

import java.util.Random;
import java.util.Date;
class Proj09{
  public static void main(String[] args){
    
    //Create a pseudo-random number generator
    Random generator = null;
    if(args.length != 0){
      generator = new Random(Long.parseLong(args[0]));
    }else{
      generator = new Random(new Date().getTime());
    };
    
    //Generate some small positive random numbers.
    int[] vals = {Math.abs((byte)generator.nextInt())%5+2,
                  Math.abs((byte)generator.nextInt())%5+2,
                  Math.abs((byte)generator.nextInt())%5+2};

    //Create an empty array object
    Object[][] array = new Object[3][];
    array[0] = new Object[vals[0]];
    array[1] = new Object[vals[1]];
    array[2] = new Object[vals[2]];

    //Instantiate an object from the student's code.
    Proj09Runner obj = new Proj09Runner();
    //Pass a reference to the empty array to the run method
    // of the object instantiated from the student's code
    // where the elements in the array object will be
    // populated with increasing Integer values.
    obj.run(array);

    //Display the data in the populated object.
    for(int i=0;i<3;i++){
      for(int j=0;j<vals[i];j++){
        System.out.print(((Object[])array[i])[j] + " ");
      }//end inner loop
      System.out.println();//new line
    }//end outer loop

    //Print some information that must descibe the
    // populated object.
    System.out.println();//blank line
    System.out.println("Row 0 width = " + vals[0]);
    System.out.println("Row 1 width = " + vals[1]);
    System.out.println("Row 2 width = " + vals[2]);
    System.out.println("Final value = " + (vals[0] + vals[1] + vals[2]));

    System.out.println("That's all folks.");

  }//end main
}//end class Proj09
//End program specifications.
import java.util.Random;
导入java.util.Date;
类别Proj09{
公共静态void main(字符串[]args){
//创建一个伪随机数生成器
随机生成器=空;
如果(args.length!=0){
生成器=新随机(Long.parseLong(args[0]);
}否则{
生成器=新随机(新日期().getTime());
};
//生成一些小的正随机数。
int[]vals={Math.abs((字节)generator.nextInt())%5+2,
Math.abs((字节)generator.nextInt())%5+2,
Math.abs((字节)generator.nextInt())%5+2};
//创建一个空数组对象
对象[][]数组=新对象[3][];
数组[0]=新对象[VAL[0]];
数组[1]=新对象[VAL[1]];
数组[2]=新对象[VAL[2]];
//从学生代码实例化一个对象。
Proj09Runner obj=新的Proj09Runner();
//将对空数组的引用传递给run方法
//从学生代码实例化的对象的
//数组对象中的元素将位于何处
//填充递增的整数值。
运行对象(数组);
//显示填充对象中的数据。

对于(int i=0;i而言,问题在于您的
run
方法在只设置第一行第一列的值之后返回得太早。它还尝试返回它不需要的
数组

实际上,我认为它根本不需要返回值,因为它只是用值填充数组。将返回类型从
Integer
更改为
void
,并删除您得到的早期
return
语句:

public void run(Object[][] array) {
    for(int i=0;i<array.length;i++){
        for(int j=0;j<array[i].length;j++){
            array[i][j] = new Integer((i+1)*(j+1));
        }
    }
}
或者,如果您想使
for
循环保持简单,您也可以这样做:

int val = 1;
for(int i=0;i<array.length;i++){
    for(int j=0;j<array[i].length;j++){
        array[i][j] = new Integer(val);
        val += 1;
    }
}
int val=1;

对于(int i=0;太好了!我有输出。但是,我认为我填充数组的方式不正确。我的第一行增加了1,第二行增加了2,第三行增加了3。我需要的输出是一行接一行不断递增的数字。如果第一行以5结尾,第二行以6开头,依此类推。关于如何更改公式以获得此输出,您有什么想法吗?非常感谢您的帮助!请查看编辑后的答案,看看这是否是您想要的。非常完美,非常感谢您的帮助。在我的课程中,我没有非常清楚地了解,当您通过方法填充数组时,我不需要返回任何内容g、 这为我澄清了很多。另外,我更喜欢第二个for循环,因为它更容易阅读,而且实现起来也很简单。谢谢!
int val = 1;
for(int i=0;i<array.length;i++){
    for(int j=0;j<array[i].length;j++){
        array[i][j] = new Integer(val);
        val += 1;
    }
}