Type mismatch: cannot convert from int to int[]

2020-04-21 06:21发布

问题:

A question I cannot seem to solve specifically for this line. I have searched for it. The assignment is to implement an application a baseball players Shirt number, batting average and number of walks. Must use a 2-D array of 20 columns and 3 rows. I am working on setting up lines to make the random numbers for the player's Number of times at bat as well as results. Yet I get a error of unable to convert from int to int[]. I have read several other questions with the same problem and none seem to help me. I can be not understanding them but still I am unable to solve the Issue. Thank you, D

import java.util.Random;

public class Stats

public static void main(String[] args) 
{

    int[][] array = { { 1, 2, 3 },
                      { 4, 5, 6 },
                      { 7, 8, 9 }, 
                      { 10, 11, 12 },
                      { 13, 14, 15 },
                      { 16, 17, 18 },
                      { 19, 20, 21 },
                      { 22, 23, 24 },
                      { 25, 26, 27 },
                      { 28, 29, 30 },
                      { 31, 32, 33 }, 
                      { 34, 35, 36 }, 
                      { 37, 38, 39 },
                      { 40, 41, 42 },
                      { 43, 44, 45 },
                      { 46, 47, 48 },
                      { 49, 50, 51 },
                      { 52, 53, 54 },
                      { 55, 56, 57 },
                      { 58, 59, 60 } };

    Random randomNumbers = new Random();

    for (int row = 0; row < array.length; row++ )
     {

         for (int column = 0; column < array[ row ].length; column++ )
         {
             int counter = 0;

             array[ counter ] = randomNumbers.nextInt() //Error here

             System.out.print( counter + "\t " + array[ counter ] + "\t");

             counter++;
         }

         System.out.println(" ");
     }

}
}

回答1:

You need to access both indexes.

array[ row ][ column ] = randomNumbers.nextInt(); //Error here

this can also be written as

int[] oneRow = array[row];
int oneCell = oneRow[column];

If you put int[] oneRow = array[row]; above your inner loop you can replace all array[row] in the inner loop with oneRow.

Besides, counter will always be 0, you reset the value to 0 each time you enter the inner loop when you declare it.

You might want to use counter, but put it outside the inner loop.

int counter = 0;
for (int column = 0; column < array[ row ].length; column++ )
{
     array[ row ][ counter ] = randomNumbers.nextInt() //Error here


回答2:

I think you have to specify all the dimension before assigning any value to array variable. For example array[0][1] instead of only array[0]



回答3:

because you are using 2d array

array[0] is an array of 3 elements, so if you try to do array[0]=0, this will error, and this is what your line is trying to do. Because, array[0] contains an array.

you need to do

array[counter][0] = shortNumber;
array[counter][1] = battingAverage;
array[counter][0] = numWalks;

obviously those values could be randoms etc.



回答4:

@Carlos Sorry for the mistake...i accidently used the c# syntax i used the Integer Class just to show why new keyword is necessary sometimes...

The Best Approach is to declare the array and initialise it at runtime

class BaseBall {
Integer[][] array ;
int noOfBaseBallPlayers;

public BaseBall(int noOfBaseBallPlayers)
{
this.noOfBaseBallPlayers = noOfBaseBallPlayers;
this.array = new Integer[noOfBaseBallPlayers][3];
}

public void assignValues()
{

Random randomNumbers = new Random();

    for (int row = 0; row < noOfBaseBallPlayers; row++ )
     {
         for (int column = 0; column < 3 ; column++ )
         {
             array[row][coloumn] = new Integer(randomNumbers.nextInt())   
         }

     }


}

}