Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要类似多维数组的东西,但使用for循环_Java_Arrays_Loops_For Loop - Fatal编程技术网

Java 我需要类似多维数组的东西,但使用for循环

Java 我需要类似多维数组的东西,但使用for循环,java,arrays,loops,for-loop,Java,Arrays,Loops,For Loop,它没有按照我想要的方式运行;我希望它像表一样运行,但只使用for循环,而不使用数组[][]。查看库集合。只需使用常规for循环即可一次迭代所有数组: String[] item ={"[1]hotdog", "[2]eggpie","[3]menudo","[4]pizza","[5]lumpia"}; int[] cost = {5, 10, 15, 20, 25}; int[] selling = {10,15,20,25,30,}; int[] qty = {2,4,6,8,10}; f

它没有按照我想要的方式运行;我希望它像表一样运行,但只使用for循环,而不使用数组[][]。

查看库集合。

只需使用常规for循环即可一次迭代所有数组:

String[] item ={"[1]hotdog", "[2]eggpie","[3]menudo","[4]pizza","[5]lumpia"};
int[] cost = {5, 10, 15, 20, 25};
int[] selling = {10,15,20,25,30,};
int[] qty = {2,4,6,8,10};

for(int b = 0; b<5;b++) {
    for(int c = 0; c<=1;c++) {
        for(int d = 0; d<=1;d++) {
            for(int e = 0; e<=1;e++) {
                System.out.println(" " + item[b] + "\t" +
                    cost[c] + "\t\t" + selling[d] + "\t\t" + qty[e]);
            }
        }
    }
}
我建议将所有这些字段放在一个对象中,然后可以迭代该对象的数组

String[] item ={"[1]hotdog", "[2]eggpie","[3]menudo","[4]pizza","[5]lumpia"};
int[] cost = {5, 10, 15, 20, 25};
int[] selling = {10,15,20,25,30,};
int[] qty = {2,4,6,8,10};
for (int i = 0; i < item.length; i++)
{
    System.out.println(" " +item[i]+"\t"+cost[i]+"\t\t"+selling[i]+"\t\t"+qty[i]);
}
控制台输出:[0]0
[0]0 0

你所做的事情似乎很随意。。。你想做什么?你不应该这样做,也不应该按你想的方式做。你应该有一个类项目的名称,订购数量和成本可能。。。背景是什么?我的教授说不要使用他没有教过的东西。我的问题是建立一个市场系统,在商品清单销售和打印上有一个菜单。关于成本销售和库存数量。我的教授说不要使用他没有教过的东西,单凭这一点就可以让你的教授在课程结束时得零分。我对你的学校一无所知,但这个答案没有用。OP应该如何处理Table?初学者呵呵,我可以在我的程序中再问一些问题吗?或者制作一个新的线程吗?@AllanPatrickCaldito这回答了你的问题吗?如果你有任何其他问题,你应该发布一个新问题。是的,它确实让我像个白痴xD
public class Item {

    private int position;
    private String name;
    private int selling;
    private int quantity;
    private int cost;

    public Item()
    {      
            position = 0;
        name="";
        selling = 0;
        quantity =0;
        cost = 0;
    }

    public String GetState()
    {
        return String.format("{0} {0} {0} {0} {0} {0}", position,name,selling,cost,quantity); 

    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSelling() {
        return selling;
    }

    public void setSelling(int selling) {
        this.selling = selling;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getCost() {
        return cost;
    }

    public void setCost(int cost) {
        this.cost = cost;
    }


}


package test;

import java.util.ArrayList;
import java.util.List;

public class ItemTest {

    public static void main(String[] args) {

        Item i = new Item();
        List<Item> items = new ArrayList<Item>();
        items.add(i);
        items.add(i);

        for (Item item : items) {

            System.out.println(item.GetState());
        }




    }

}