如何添加两个不同的数组并打印结果?

时间:2016-12-29 03:23:42

标签: java arrays multidimensional-array

我必须制作一个计算电影院座位的阵列,第二个m2代表另一个播放时间。我不知道如何将两个数组一起添加并打印这两个数组之和的结果。

这是MovieSeats测试人员档案:

public class MovieSeatsTester
{

    public static void main(String[] args)
    {
        MovieSeats m = new MovieSeats(3,3);
        MovieSeats m2 = new MovieSeats(3,3);
        m.seating(0,0);
        m.seating(0,1);
        m.seating(1,0);
        m.seating(2,2);
        m.seating(2,2);
        m2.seating(0,0);
        m2.seating(0,0);
        m.print();
        m.reset();
        m2.print();
        m2.reset();
    }
}

这是MovieSeats文件:

public class MovieSeats
{
    private int attendance[][];

    public MovieSeats()
    {
    }

    public MovieSeats(int rows, int columns)
    {
        attendance = new int[rows][columns];        
    }

    public void seating(int r, int c)
    {   
        attendance[r][c] += 1;
    }

    public void print()
    {
    for (int r = 0; r < attendance.length; r++)
    {
        for (int c = 0; c < attendance.length; c++)
        {
            System.out.println("At row " + r + " col " + c + ", There are " +   attendance[r][c] + " Sitting here.");
        } 

    }
        System.out.println();
    }

    public void reset()
    {
    for (int r = 0; r < attendance.length; r++)
    {
        for (int c = 0; c < attendance.length; c++)
        {
            attendance[r][c] = 0;
        } 
    }
    System.out.println();
    }
}    

2 个答案:

答案 0 :(得分:0)

我假设通过&#34;添加两个不同的数组&#34;你的意思是&#34;创建第三个数组,其中第三个数组中的每个元素是前两个数组中每个数组的相应元素的总和。&#34;

1。访问每个数组

为了将两个数组一起添加,首先需要以某种方式访问​​每个数组。由于attendance字段为private,因此您应该为MovieSeats类添加getter方法。

在没有封装的情况下获取attendance

public int[][] getAttendance() {
    return attendance;
}

使用封装获取attendance

public int[][] getAttendance() {
    int rows = attendance.length();
    int cols = attendance[0].length();
    int[][] copy = new int[rows][cols];

    for(int i = 0; i < rows; i++)
        for(int j = 0; j < cols; j++)
            copy[i][j]=current[i][j];

    return copy;
}

2。添加两个阵列Element-Wise

接下来,一旦您可以访问同一位置(可能在main内)的两个阵列,您就必须将它们一起添加。我建议将结果放在第三个数组中。添加这两个数组的最基本方法如下:

int[][] attendance1 = m.getAttendance();
int[][] attendance2 = m2.getAttendance();
int[][] sum = new int[3][3];

for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        sum[i][j] = attendance1[i][j] + attendance2[i][j];

然后,您可以按座位打印出席人数:

for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        System.out.printf("Total Attendance for seat (%d,%d): %d\n",
                i, j, sum[i][j]);

现在,如果您想要做的是将两个数组的所有元素相加为单个值,那么您将执行以下操作。

Sum An Array

int sum = 0;

int[][] attendance1 = m.getAttendance();
for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        sum += attendance1[i][j];

int[][] attendance2 = m2.getAttendance();
for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        sum += attendance2[i][j];

答案 1 :(得分:0)

您可以将以下方法添加到MovieSeat类:

public int sum() {
    int result = 0;
    for (int r = 0; r < attendance.length; r++) {
        for (int c = 0; c < attendance.length; c++) {
            result += attendance[r][c];
        }            
    }
    return result;
} 

调用sum方法并在main方法中找到总和:

MovieSeats m = new MovieSeats(3, 3);
MovieSeats m2 = new MovieSeats(3, 3);
m.seating(0, 0);
m.seating(0, 1);
m.seating(1, 0);
m.seating(2, 2);
m.seating(2, 2);
m2.seating(0, 0);
m2.seating(0, 0);
m.print();

System.out.println("sum: " + m.sum());
System.out.println("---");

m2.print();
System.out.println("sum: " + m2.sum());

System.out.println("total sum: " + (m.sum() + m2.sum()));
m.reset();
m2.reset();

请注意在调用sum方法后重置。