Java数组问题

发布于 2024-12-15 04:45:24 字数 378 浏览 0 评论 0原文

我将如何创建一个包含 5 个项目的数组,然后为每个项目创建一个数组?我知道如何创建一个包含 5 个项目的数组,但我遇到的问题是为每个项目创建一个数组。我假设我需要 5 个数组,因为有 5 个项目。

 int gas = 0;
 int food = 0;
 int clothes = 0;
 int entertainment = 0;
 int electricity = 0;
 int[] budget = new int[4];
 budget[0] = gas;
 budget[1] = food;
 budget[2] = clothes;
 budget[3] = entertainment;
 budget[4] = electricity;

提前致谢

How would I go about creating an array of say 5 items and then after create an array for each item?. I know how to create an array of 5 items but the problem I have is creating an array for each one after. Im assuming I would need 5 arrays since there's 5 items.

 int gas = 0;
 int food = 0;
 int clothes = 0;
 int entertainment = 0;
 int electricity = 0;
 int[] budget = new int[4];
 budget[0] = gas;
 budget[1] = food;
 budget[2] = clothes;
 budget[3] = entertainment;
 budget[4] = electricity;

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧人哭 2024-12-22 04:45:24

与其创建一个二维数组来保存逻辑上分组在一起的金额类型(我假设每月),不如定义一个数据持有者类,以便您可以使用其名称而不是容易出错的名称来访问金额指数。

例如(减去 getter 和 setter):

public class Budget {
    public int gas = 0;
    public int food = 0;
    public int clothes = 0;
    public int entertainment = 0;
    public int electricity = 0;
};

// ....
Budget[] months = new Budget[12];

budget[0].gas = gasCosts;
budget[0].food = foodCosts;
// etc

Instead of creating a 2-dimensional array holding types of amounts that are logically grouped together (I assume per month) it would be better to define a data-holder class so that you can access the amounts using their name instead of an error-prone index.

For example (minus the getters and setters):

public class Budget {
    public int gas = 0;
    public int food = 0;
    public int clothes = 0;
    public int entertainment = 0;
    public int electricity = 0;
};

// ....
Budget[] months = new Budget[12];

budget[0].gas = gasCosts;
budget[0].food = foodCosts;
// etc
温柔一刀 2024-12-22 04:45:24

你是说你想创建一个二维数组吗?预算数组中的每个元素都是另一个数组?

你会使用一个循环。

int[] budget = new int[5]; //There are 5 elements to be stored in budget, not 4
for (int y = 0; y < 5; y++) {
     budget[y] = new int[5];
}

Are you saying you want to create a 2D array? Each element in the budget array is another array?

You'd use a loop.

int[] budget = new int[5]; //There are 5 elements to be stored in budget, not 4
for (int y = 0; y < 5; y++) {
     budget[y] = new int[5];
}
和影子一齐双人舞 2024-12-22 04:45:24

也许你需要一个矩阵。

int[][] budget = new int[4][4];

在第一个索引中保留预算,在第二个索引中保留五个(在上述情况下)预算项目。
当你有一个矩阵 [x][y] 时,你有 x+1 个数组,每个数组都有 y+1 个元素。

Maybe you need a matrix.

int[][] budget = new int[4][4];

In the first index you keep the budget and in the second index the five (in the case above) budget items.
When you have a matrix [x][y] you have x+1 arrays each of wich with y+1 elements.

╰◇生如夏花灿烂 2024-12-22 04:45:24

数组中有 5 个元素。

    int[] budget = new int[5];
    budget[0] = gas;
    budget[1] = food;
    budget[2] = clothes;
    budget[3] = entertainment;
    budget[4] = electricity;

您需要二维数组,它基本上是数组的数组。二维数组由两对 [] 声明。在以下示例中,每个预算都有 10 个详细信息。

    String[][] detail = new String[budget.length][10];

There are 5 elements in the array.

    int[] budget = new int[5];
    budget[0] = gas;
    budget[1] = food;
    budget[2] = clothes;
    budget[3] = entertainment;
    budget[4] = electricity;

You need 2-dimensional array, which is basically an array of array. The 2D array is declared by 2 pairs of []. In the following example, each budget have 10 details.

    String[][] detail = new String[budget.length][10];
纸伞微斜 2024-12-22 04:45:24

如果我理解正确的话,你想要一个类似于...的二维数组,

int gas = 0;
int food = 1;
int clothes = 2;
int entertainment = 3;
int electricity = 4;
int maxEntries = 10;

int[][] myArray = new int[5][maxEntries];

可以通过以下方式访问:

myArray[gas][entryNumber] = 6;
int value = myArray[gas][entryNumber];

但这有多不灵活?您必须提前知道每个“类别”将有多少条目,或者具有在添加项目时检查给定数组长度的代码,在需要更大的数组时创建新数组(并复制旧数据)进入其中)。

您可能至少想要的是 ArrayListArrayList

ArrayList<ArrayList<Integer>> my2dArrayList = 
    new ArrayList<ArrayList<Integer>>();
...
my2dArrayList.get(gas).add(someValue);
int myValue = my2dArrayList.get(gas).get(index);

或者您的 ArrayListHashMap可以通过您的类别名称访问:

HashMap<String, ArrayList<Integer>> myMap = 
    new HashMap<String, ArrayList<Integer>>();
...
myMap.get("gas").add(someValue);
int myValue = myMap.get("gas").get(index);

If I'm understanding you correctly, you want a 2d array that would be something like ...

int gas = 0;
int food = 1;
int clothes = 2;
int entertainment = 3;
int electricity = 4;
int maxEntries = 10;

int[][] myArray = new int[5][maxEntries];

This could be accessed via:

myArray[gas][entryNumber] = 6;
int value = myArray[gas][entryNumber];

But how inflexible this is? You have to know in advance how many entries there will be for each "category" or have code that checks a given array length when you're adding an item, creating an new array when you need a larger one (and copying the old data into it).

What you probably want at the very least is a ArrayList of ArrayLists:

ArrayList<ArrayList<Integer>> my2dArrayList = 
    new ArrayList<ArrayList<Integer>>();
...
my2dArrayList.get(gas).add(someValue);
int myValue = my2dArrayList.get(gas).get(index);

Or a HashMap of ArrayLists you could access via the names of your categories:

HashMap<String, ArrayList<Integer>> myMap = 
    new HashMap<String, ArrayList<Integer>>();
...
myMap.get("gas").add(someValue);
int myValue = myMap.get("gas").get(index);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文