gpt4 book ai didi

java - 使用类声明用整数和字符串填充数组

转载 作者:行者123 更新时间:2023-12-01 16:47:56 24 4
gpt4 key购买 nike

因此,我不确定在根据我最初声明的已建立数组打印数组时缺少哪一步。我知道 java 有其他方法来打印数组,但我试图使步骤更长(而不是使用 Java 快捷方式),以便更容易将相同的方法应用于其他语言。如果有人可以看看我所拥有的并告诉我我缺少什么,我将不胜感激。

更新:我觉得很愚蠢,因为这是一个简单的解决方案,但现在我正在处理另一个问题。我需要数组来接受字符串和整数,因此我创建了一个包含公共(public)类型单词和数字的类,但我不确定如何正确填充数组。我知道我没有设置数字来填充数组,但如果有人可以帮助我找出一个,我可以自己做另一个。

   package employeetotalhours;

import java.util.ArrayList;

public class employeetotalhours { // main class declaration

public static class Table{ // sub class declaration
public int number; // establish input field values for ints
public String word; // establish input field values for strings
Object array[];
}

public static void main(String[] args){ // main method

Table[] table = new Table[9];

Table word = null;

table[0] = word;

ArrayList<Table> Table = new ArrayList<Table>();

Table.add(new Table());

// {{"Employee ","Su","M","T","W","T","F","Sa","Total"},
// {"EmpID 0 ","4 ","2","5","3","4","5","8 ", "0"},
// {"EmpID 1 ","1 ","7","8","4","3","3","4 ", "0"},
// {"EmpID 2 ","2 ","3","3","4","3","3","2 ", "0"},
// {"EmpID 3 ","3 ","3","3","7","3","4","1 ", "0"},
// {"EmpID 4 ","1 ","3","2","4","3","6","3 ", "0"},
// {"EmpID 5 ","5 ","3","4","4","6","3","4 ", "0"},
// {"EmpID 6 ","8 ","3","7","4","8","3","8 ", "0"},
// {"EmpID 7 ","2 ","6","3","5","9","2","7 ", "0"}};
// all array data, multiple lines can be used as long as code is not closed with ;

int rows = 9; // establish array row size

int columns = 9; // establish array column size

for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
System.out.print(table[i] + " ");
}
System.out.println();
}
}
}

最佳答案

在打印所有值的循环之前,有以下循环:

for(int i = 0; i < rows; i++) // loop to cycle through cells in array table
for(int j = 0; j < columns; j++)
array[i][j] = 0;

在此循环中,您将数组的每个元素设置为 0。
所以当你打印元素时,一切都是0

关于java - 使用类声明用整数和字符串填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46307639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com