使用二维整数数组

时间:2014-09-20 23:49:40

标签: java arrays math

基本上,我需要提示用户输入数组的行和列,而不询问有多少行和列。如何改进以下代码呢?

    System.out.println("Please input the first set of integers separated by spaces" );
    String givenSet1 = console.readLine();

    set1 = givenSet1.split(" ");
    set1Values = new int[set1.length];

    for(int i = 0; i < set1.length; i++)
    {
        set1Values[i] = Integer.parseInt(set1[i]);
    }

    while(counter <= set1Values.length)
    {
        numbers = new int[set1Values.length][];
        numbers[counter] = set1Values[counter];
    }

    for(int a = 0; a < set1Values.length; a++)
    {
        System.out.println("Please input the set of integers that are to be associated with    element " + a + ", separated by spaces" );
        String givenSet2 = console.readLine();

        set2 = givenSet2.split(" ");
        set2Values = new int[set2.length];

        numbers[a] = new int[set2Values.length];
        System.out.println(numbers[a]);

        for(int b = 0; b < set2Values.length; b++)
        {

        }
    }

2 个答案:

答案 0 :(得分:0)

您可以使用Array.length()函数返回数组中的元素数。

答案 1 :(得分:0)

你的问题不清楚,很难理解你在代码中想要做什么,但是我在黑暗中拍摄了......

好像你想根据用户的输入创建一个二维数组。用户输入的总数量将定义数组中的列数,用户提供一组整数作为每个列的相关数字存储。结果将是一个二维数组,只要用户总是输入相同数量的数字与每列相关联,但只是因为我编码它来创建一个锯齿状数组。

代码:

import java.util.Scanner;  

public class Create2DArray {

    public static void main(String[] args)
    {

        int[][] jaggedArray; //declare 2D array
        String[] userInput;  //declare array for user input
        String[] userInput2; //declare array for ints to associate with first input

        Scanner input = new Scanner(System.in); //scanner to read 

        System.out.println("Please input the first set of integers separated by spaces: " );      
        userInput = input.nextLine().split(" "); //parsing first set of string input

        jaggedArray = new int[userInput.length][];  //create 2D array based on no. of columns defined in first set of user input       

        // array elements will be referenced by x and y, like coordinates.
        for (int x = 0; x < jaggedArray.length; x++) //cycle through first set of numbers to prompt for associate numbers
        {
            System.out.println("Please input the set of integers that are" +
                               "to be associated with element " + userInput[x] + ", separated by spaces: " );
            userInput2 = input.nextLine().split(" "); //parse and store the set to be associated with userInput[x] 

            jaggedArray[x] = new int[userInput2.length + 1]; //create a new int array in column x of jagged array, +1 to hold userInput[x] then associated values

            for (int y = 0; y < jaggedArray[x].length; y++) // cycle through the new array @ column x
            {
                if (y == 0)
                    jaggedArray[x][y] = Integer.parseInt(userInput[x]); //if y = 0 then copy the "column header" first
                else
                    jaggedArray[x][y] = Integer.parseInt(userInput2[y-1]); // else copy the new elements at position y-1 due to jagged array being +1 more than userInput2 array
            }       
        }      


        System.out.println();

        for (int x = 0; x < jaggedArray.length; x++) //cycling through the array to print it all
        {
            System.out.print("For integer: " + jaggedArray[x][0] + 
                             " the following integers are associated with it. ");

            for (int y = 1; y < jaggedArray[x].length; y++)
            {
                System.out.print(jaggedArray[x][y] + " ");                
            }     

            System.out.println();                   
        }      
    }
}

下次请尝试提供有关所需最终结果的更多信息以及更完整的示例代码。如果我们知道某些事情(比如示例中的某些变量类型),那么理解您的代码和要求会稍微容易一些。信息越多越好。

希望这不会太难以理解,而这正是你真正想要的。