简体   繁体   English

声明并初始化矩阵中的元素 Java

[英]Declare and initialize elements in a matrix Java

I find it challenging to initialize elements in a matrix (2d array).我发现初始化矩阵(二维数组)中的元素具有挑战性。 I know how to declare a 2d array and prompt user for input and then print it, but if I have a 2 d array with 5 rows and I in every row want that column 0 is articleNumber, column 1 is price and column 2 is amount I fail to connect the userinput with the "variable".我知道如何声明一个二维数组并提示用户输入然后打印它,但是如果我有一个有 5 行的二维数组并且我在每一行中都希望第 0 列是文章编号,第 1 列是价格,第 2 列是金额我无法将用户输入与“变量”连接起来。

The task I want to practice and understand is described this way.我想练习和理解的任务就是这样描述的。

"create a matrix that contains information about 5 articles. Each article (row) must contain the article number, price, and number. Show how to declare, assign and print the contents of the matrix (all columns for all rows)." “创建一个包含 5 篇文章信息的矩阵。每篇文章(行)必须包含文章编号、价格和编号。展示如何声明、分配和打印矩阵的内容(所有行的所有列)。”

I would be very thankful if someone could help me out with the initializing.如果有人可以帮助我进行初始化,我将非常感激。 This is what I did so far:这是我到目前为止所做的:

import java.util.Scanner;

public class TwoDArrays 
{

    public static void main(String[] args)
    {
        Scanner s1 = new Scanner (System.in); // create the scanner object
        
        // Variables
        
        int r = 0;
        int c = 0;
        int [][] twodArray; // declare the 2d array object
        
        // prompt user for input
        
        System.out.print("How many rows do you want? ");
        
        r=s1.nextInt();  
        
        System.out.print("How many columns do you want? ");
        
        c=s1.nextInt(); 
        
        
        
        twodArray = new int [r][c]; // the 2d array size is defined by user
        
        
        // insert value.
        
        
        System.out.println("Please insert values ");
        
        for(int i=0; i<r; i++) 
        {
            
            for(int j=0; j<c; j++) 
            {
                twodArray[i][j]=s1.nextInt();
                
            }
            
            System.out.println();
            
        }
        
        
        // Prints out values
        
        System.out.println("Here is what you entered");
        
        for(int i=0; i<r; i++) 
        {
            
            for(int j=0; j<c; j++) 
            {
                System.out.print(twodArray[i][j] + " "+" ");
                
            }
            
            System.out.println();
            
        }
    }
}

There is no need for you to ask how many rows and columns the user wants to enter because those are fixed values specified by your assignment: You need 5 rows - 1 for each item, and 3 columns for the 3 attributes each item has.您无需询问用户想要输入的行数和列数,因为这些是您的作业指定的固定值:您需要 5 行 - 每个项目 1 行,每个项目的 3 个属性需要 3 列。

You first declare your array as您首先将您的数组声明为

int[][] items = new int[5][3];

And you could then just initialize/fill that array with a simple loop:然后你可以用一个简单的循环初始化/填充该数组:

for(int i=0; i<items.length; i++) {
    System.out.println("Enter Data for item " +i);
    System.out.println("Enter article number:");
    items[i][0] = s1.nextInt();  
    System.out.println("Enter price:");
    items[i][1] = s1.nextInt();
    System.out.println("Enter number:");
    items[i][2] = s1.nextInt();  
}
    

Of course you could still use a nested loop to fill the columns of each item.当然,您仍然可以使用嵌套循环来填充每个项目的列。 But with a fixed value of 3 I really see no reason to do that.但是对于固定值 3,我真的认为没有理由这样做。

Edit:编辑:

It should be noted that using a 2D array like this is a really bad choice for a data structure.应该注意的是,使用这样的二维数组对于数据结构来说是一个非常糟糕的选择。 Your assignment specifically asks for that but in reality you would be much better of creating a class for the Item:您的作业特别要求这样做,但实际上您最好为项目创建 class:

public class Item {
    private int articleNumber;
    private BigDecimal price;
    private int number;
    
    public Item() {
    }
    
    public int getArticleNumber() {
        return articleNumber;        
    }
    
    public void setArticleNumber(int articleNumber) {
        this.articleNumber = articleNumber;        
    }

    public BigDecimal getPrice() {
        return price;
    }
    
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
    
    public int getNumber() {
        return number;
    }
    
    public void setNumber(int number) {
        this.number = number;
    }
}

And then you would use a List or Array of that custom class然后您将使用该自定义 class 的列表或数组

final List<Item> itemList= new ArrayList<>();

And using your getters/setters to set the attributes of each item并使用您的 getter/setter 设置每个项目的属性

final Item newItem = new Item();
itemList.add(newItem);
    
System.out.println("Enter article number:");
newItem.setArticleNumber(s1.nextInt());  
// ... etc pp. for the other attributes

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

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