用Java动态创建全局多维数组

时间:2017-02-09 18:11:13

标签: java php arrays multidimensional-array

我有一个订购系统,它在数据最终提交并存储在数据库之前包含许多步骤。我已经完成并实现了相同订购系统的网络版。下面是PHP中的多维数组,我根据以下值动态创建。

在订单的第一步中,选择计划。根据该计划,将决定总天数。

计划1 - 服务天数26
计划1 - 每日服务2
计划1 - 每天服务的茶点2

计划2 - 服务天数5
计划2 - 每天供应的膳食3
计划2 - 每天服务的茶点0

等等...

在第二步中,选择订单的开始日期。周末将被排除在外,只有工作日将被计算为服务天数。

动态生成的PHP多维数组低于

Array
(
    [Day 1] => Array
        (
            [meal_id_1] => Unique ID //to be replaced with user selection
            [meal_code_1] => Meal Name //to be replaced with user selection
            [meal_type_1] => Meal //prefilled based on the selected package
            [meal_id_2] => Not Available //to be replaced with user selection
            [meal_code_2] => 2 //to be replaced with user selection
            [meal_type_2] => Meal //prefilled based on the selected package
        )

    [Day 2] => Array
        (
            [meal_id_1] => Unique ID //to be replaced with user selection
            [meal_code_1] => Meal Name //to be replaced with user selection
            [meal_type_1] => Meal //prefilled based on the selected package
            [meal_id_2] => Not Available //to be replaced with user selection
            [meal_code_2] => 2 //to be replaced with user selection
            [meal_type_2] => Meal //prefilled based on the selected package
        )

在上面的代码中,动态添加了天数,并且动态添加了meal_id_1, meal_code_1 and meal_type_1中的数值。

要以逻辑方式连接应用程序和Web应用程序,我想在类似的数组中发布应用程序中的选择。

由于我根据计划选择了膳食和茶点,因此我将为第1天加载膳食,然后根据计划选择第1天的茶点。将有1个活动供膳食加载更新日期编号和茶点相同。

使用以下代码,我可以获得在ArrayList中选择的膳食的唯一ID。

int count = 0;
int size = list.size();

List<String> selected_meals = new ArrayList<String>();
    for (int i = 0; i<size; i++){
        if (list.get(i).isSelected()){
            count++;

            String selected_meal_string = list.get(i).getMeal_id();

            selected_meals.add(selected_meal_string);
        }
    }

如何将此选择转移到全局多维数组,以便在最后一步中我可以发布它以保存在数据库中?

1 个答案:

答案 0 :(得分:1)

根据我的评论,我认为您真的想在这里使用课程,请参阅下面的示例以帮助您入门。您可能需要对OOP(面向对象编程)如何工作进行一些研究。

public class Meal {

//I dont know what type of data each attribute is supposed to be so I chose ints. Feel free to change.
private int mealId;
private int mealCode;
private int mealType;

public Meal(int mealId, int mealCode, int mealType){
    this.mealId = mealId;
    this.mealCode = mealCode;
    this.mealType = mealType;
}

public int getMealId() {
    return mealId;
}

public int getMealCode() {
    return mealCode;
}

public int getMealType() {
    return mealType;
}
}

现在是Day课程:

import java.util.ArrayList;

public class Day {
private ArrayList<Meal> meals = new ArrayList<>();

public Day(Meal...meals){
    //This uses magic params to allow you to pass in as many meals as you want.
    for(Meal meal : meals){
        this.meals.add(meal);
    }
}

public ArrayList<Meal> getMeals() {
    return meals;
}
}

现在主要方法是:

import java.util.ArrayList;

public class Control {
public static void main(String [] args){
    ArrayList<Day> days = new ArrayList<>();

    //Create your meals.
    Meal meal1 = new Meal(1, 1, 1);
    Meal meal2 = new Meal(2, 3, 4);

    //Add the meals to a day.
    Day day1 = new Day(meal1, meal2);

    //Add the day to the list of days.
    days.add(day1);

    //Getting the meal code for the first meal on the first day. This looks complex, but you would likely break it down before getting values.
    System.out.println(days.get(0).getMeals().get(0).getMealCode());
}
}
相关问题