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

时间:2022-04-25 21:29:44

I have an Ordering System that comprises of a number of steps before the data is finally submitted and stored in the database. I have already completed and implemented the web version of the same Ordering System. Below is the Multidimensional Array in PHP that I created dynamically based on the below values.

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

In the first step of Order, a Plan is to be selected. Based on that plan, the total number of days will be decided.

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

Plan 1 - Days Served 26
Plan 1 - Meals Served Per Day 2
Plan 1 - Refreshments Served Per Day 2

Plan 2 - Days Served 5
Plan 2 - Meals Served Per Day 3
Plan 2 - Refreshments Served Per Day 0

and so on...

计划1 - 服务天数26计划1 - 每日服务2计划1 - 每日服务的茶点2计划2 - 服务日5计划2 - 每日服务3计划2 - 每日服务的茶点0等等...

In the second step, starting date of the Order is to be selected. Weekends are to be excluded and only Weekdays will be counted as days served.

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

The PHP Multidimensional Array generated dynamically is below

动态生成的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
        )

In the above code, day number is added dynamically and numeric value in meal_id_1, meal_code_1 and meal_type_1 is also added dynamically.

在上面的代码中,动态添加日期编号,并且还动态添加meal_id_1,meal_code_1和meal_type_1中的数值。

To connect the App and Web Application logically, I want to post the selection from the App in similar Array.

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

Since I have Meals and Refreshments to be selected based on the plan, therefore I will be loading Meals for Day 1 and then based on the Plan selected Refreshments for Day 1. There will be 1 Activity for Meals, which be loaded with updated Day number and same for the Refreshments.

由于我根据计划选择了膳食和茶点,因此我将在第1天加载膳食,然后根据计划选择的第1天的茶点。将有1个膳食活动,其中加载了更新的日期号码和茶点一样。

Using the below code, I am able to get the Unique ID of the Meals selected in an ArrayList.

使用下面的代码,我可以获得在ArrayList中选择的Meals的唯一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);
        }
    }

How can I transfer this selection to a Global Multidimensional Array so that in the final step I can post it to be saved in the database?

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

1 个解决方案

#1


1  

as per my comment I think you are really looking to use a class here, please see the example below to get you started. You may require some research into how OOP (Object Oriented Programming) works though.

根据我的评论,我认为你真的想在这里使用一个课程,请看下面的例子来帮助你入门。您可能需要对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;
}
}

Now the Day class:

现在是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;
}
}

Now wherever your main method is:

现在,无论您的主要方法是什么:

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());
}
}

#1


1  

as per my comment I think you are really looking to use a class here, please see the example below to get you started. You may require some research into how OOP (Object Oriented Programming) works though.

根据我的评论,我认为你真的想在这里使用一个课程,请看下面的例子来帮助你入门。您可能需要对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;
}
}

Now the Day class:

现在是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;
}
}

Now wherever your main method is:

现在,无论您的主要方法是什么:

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());
}
}