简体   繁体   English

使用二维整数数组

[英]using two dimensional integer arrays

Basically, I need to prompt the user for the rows and columns of an array without asking how many rows and columns there are. 基本上,我需要提示用户输入数组的行和列,而无需询问有多少行和列。 How do I improve the following code to do that? 我如何改进以下代码来做到这一点?

    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++)
        {

        }
    }

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

Your question is unclear and it is difficult to understand what you are trying to do from your code but I took a shot in the dark... 您的问题尚不清楚,并且很难从代码中了解您要执行的操作,但是我在黑暗中开枪了...

It seems as if you want to create a two dimensional array based on the user's input. 似乎您想基于用户的输入创建一个二维数组。 The total amount of numbers inputted by the user would define the amount of columns in the array, with the user providing a set integers to store as associated numbers for each of those columns. 用户输入的数字总数将定义数组中的列数,用户提供一组整数以存储为这些列中每列的关联数字。 The result would be a two dimensional array as long as the user always entered the same amount of numbers to associate with each column, but just incase I coded it to create a jagged array. 只要用户始终输入相同数量的数字来与每一列相关联,结果就是二维数组,但以防万一我将其编码以创建锯齿状数组。

Code: 码:

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();                   
        }      
    }
}

Next time please try to provide more information about your desired end results as well as more complete example code. 下次,请尝试提供有关所需最终结果的更多信息以及更完整的示例代码。 It would be slightly easier to understand your code and requirements if we knew certain things such as what type of objects some of the variables in your example were. 如果我们知道某些事情,例如示例中的某些变量是什么类型的对象,那么理解您的代码和要求会稍微容易一些。 The more information the better. 信息越多越好。

Hopefully this won't be too hard to follow, and it's what you actually wanted. 希望这不会太难遵循,而这正是您真正想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM