gpt4 book ai didi

java - 使用方法填充二维数组时如何修复Array Errors和ArrayStoreException

转载 作者:行者123 更新时间:2023-12-02 11:06:49 26 4
gpt4 key购买 nike

我在下面粘贴了我的代码。
数组确实为我提供了随机数,但是在数组末尾会 pop 错误,并且我无法修复错误。
目标是产生一个具有2列和n个行的数组。随机整数将填充数组。
我有一个主控件,在其中创建了一个二维数组来保存值,并且我使用Array.fill()方法调用了我制作的randArray方法,该方法使用随机整数构造了实际的数组。

问题是一旦代码运行,它给了我一些我无法摆脱的错误。这些是错误:

Exception in thread "main" java.lang.ArrayStoreException: [[I
at java.base/java.util.Arrays.fill(Arrays.java:3639)
at Animal.main(Animal.java:22)

我尝试过的事情:
  • 将范围更改为range-1
  • 将行更改为第1行
  • 在第二个方法
  • 中更改i和j值
  • 将我的教科书扔进它所属的垃圾中

  • 我应该朝哪个方向前进?任何帮助将非常感激。
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Random; //import library for generating random numbers

    //Creating an array with 2 columns, the number of rows are based on user input and the numbers
    //inside the array are randomized between 0 and x with user giving x

    public class Animal {

    public static void main(String [] args)
    {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the number of rows: ");
    int numNodes = input.nextInt(); //this will be the number of rows in the array

    System.out.println("Enter the maximum: ");
    double max = input.nextDouble(); //array data is inside this range


    int[][] something = new int [numNodes][];
    Arrays.fill(something, randArray(numNodes,max));



    // System.out.println("The rows are: " + arrayMade(numNodes,networkSize)); //testing the array to see if it works

    System.out.println(something);
    }

    /** Creating an array to fill the random array with random ints
    *
    * @param numRows the number of rows the array will have
    * @param range the maximum for a value in the array
    * @return randArray returns a randomized filled in 2-d array
    */

    public static int[][] randArray (int numRows, double range) //method to make the array
    {
    int[][] randArray = new int[numRows+1][2];
    Random rand = new Random();

    int count = numRows-1;
    int i = 0;

    while(i < count){ //this loops based on how many rows there are

    for (int j = 0; j < 1; j++)
    {
    randArray[i][0] = rand.nextInt((int)range) ; // random integer from 0 to max
    randArray[i][1] = rand.nextInt((int)range) ;

    System.out.println(randArray[i][0] + " " + randArray[i][1]); //test line
    i++;

    }//end for-loop


    }//end while-loop

    return randArray;
    }//end arrayMade


    }//end class

    最佳答案

    您不必使用Arrays.fill,只需将randArray函数的返回值分配给2d数组。

    int[][] something = randArray(numNodes, max);

    PS:您需要一种将2d数组打印到控制台的方法,使用 System.out.println(something)将输出意外结果。

    另外,删除 for (int j = 0; j < 1; j++)循环,这确实是不必要的。

    这是您可以引用的最终代码:

    import java.util.Random;
    import java.util.Scanner;

    //Creating an array with 2 columns, the number of rows are based on user input and the numbers
    //inside the array are randomized between 0 and x with user giving x

    public class Animal {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the number of rows: ");
    int numNodes = input.nextInt(); //this will be the number of rows in the array

    System.out.println("Enter the maximum: ");
    int max = input.nextInt(); //array data is inside this range


    int[][] something = randArray(numNodes, max);


    // System.out.println("The rows are: " + arrayMade(numNodes,networkSize)); //testing the array to see if it works

    System.out.println(something);
    }

    /**
    * Creating an array to fill the random array with random ints
    *
    * @param numRows the number of rows the array will have
    * @param range the maximum for a value in the array
    * @return randArray returns a randomized filled in 2-d array
    */

    public static int[][] randArray(int numRows, int range) //method to make the array
    {
    int[][] randArray = new int[numRows + 1][2];
    Random rand = new Random();

    int count = numRows - 1;
    int i = 0;

    while (i < count) { //this loops based on how many rows there are

    randArray[i][0] = rand.nextInt(range); // random integer from 0 to max
    randArray[i][1] = rand.nextInt(range);

    System.out.println(randArray[i][0] + " " + randArray[i][1]); //test line
    i++;

    }//end while-loop

    return randArray;
    }//end arrayMade


    }//end class

    关于java - 使用方法填充二维数组时如何修复Array Errors和ArrayStoreException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60646527/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com