Я вставил свой код ниже. Массив действительно дает мне случайные числа, однако в конце массива появляются ошибки, и я не смог их исправить. Цель состоит в том, чтобы создать массив с 2 столбцами и n количеством строк. рандомизированные целые числа заполнят массив. У меня есть основной, в котором я создал 2-й массив для хранения значений, и я использую метод 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)
Вещи, которые я пробовал:
- изменить диапазон на диапазон-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