¿Cómo cambiar la matriz 2d por entrada?

I have a 2D array of 5 rows and 5 columns, all filled with the value 0. How can I make my program do this:

  1. Enter any random combination of row and column like 2-5, without the [] brackets. Just typing 2-5 should be enough to make my program understand I mean row 2 column 5

  2. Assign value I enter to said array in row-column combination.

This is what I got so far. As you can see I have only managed to output the values of all array elements.

import java.util.*;

public class stink {

    public static void main(String[]args){
        int[][] kuk = new int[5][5];

    public static void printMatrix(int[][] matrix)
        for (int row = 0; row < matrix.length; row++)
            for (int col = 0; col < matrix[row].length; col++)
                System.out.printf("%2d", matrix[row][col]);

preguntado el 22 de septiembre de 12 a las 23:09

There is nothing as such 2D array in Java.. It's an Array of Array.. -

I think the issue you are facing here is to get rows and columns from 2-3 input.. You can use String.split () -

To start, do you know how to get input from the user? I suggest you first store it as a String. We can proceed from there to turning the input into ints for use with your array. -

3 Respuestas

you should use Scanner class from java API to get the inputs from the user like in the below code. pass the inputs with a delimiter like if you want to have 2X3 array pass like 2-3 where '-' is a delimiter. here are the links for Cordón el y escáner java API.

        Scanner sc = new Scanner(System.in);
    System.out.println("please enter two numbers");
    String inputs = sc.next();
    int a=Integer.valueOf(inputs.split("-")[0]);
    int b=Integer.valueOf(inputs.split("-")[1]);;
    System.out.println(a + " " + b);
    int[][] x = new int[a][b];

Respondido el 23 de Septiembre de 12 a las 00:09

@chaitanya10... By providing direct answer to the question, you are not helping him learn.. It's always better to provide resource, so that one can see and learn even more than required.. - Rohit jainista

@RohitJain hann,... you are right.tbh i dont myself think this is the optimal solution. this can be done in many other ways... - PermGenError

this is not a copy/paste ready answer, but it should at least give you an indication on how you could handle this.

        int rows = 0;
        int columns = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Rows: ");
        rows = scan.nextInt();
        System.out.println("Columns: ");
        columns = scan.nextInt();           
        int[][] kuk = new int[rows][columns];

Respondido el 22 de Septiembre de 12 a las 23:09

import java.util.*;
public class stink
    public static void main(String[]args)
        int[][] kuk = new int[5][5];
        // Where x and y are keys, integer is the integer you want to push
        pushIntoMatrix(kuk, x, y, integer);
        //kuk = pushIntoMatrix(kuk, x, y, integer); // Use if you want the method to return a value.
    public static void pushIntoMatrix(int[][] matrix, int x, int y, int integer)
    //public static int[][] pushIntoMatrix(int[][] matrix, int x, int y, int integer) // Use if you want to return the array.
        matrix[x][y] = integer;
        //return matrix; // Use if you want to return the array.

Since as you know, any data type in Java that's not a primitive is a reference, passing the kuk array into the method would affect the actual array reference. You can set a return in pushIntoMatrix() if you want, but you don't have to.

Respondido el 22 de Septiembre de 12 a las 23:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.