【问题标题】:How to push/get data from Firebase and add to a bidimensional array using Java?如何从 Firebase 推送/获取数据并使用 Java 添加到二维数组?
【发布时间】:2021-09-14 03:58:36
【问题描述】:

我已经有可以从数组[][] 计算数据的 TOPSIS 算法。现在我将使用以下数据:

虚拟数据:

double[][] data= {
                {887, 475, 4, 128, 186, 3621000},
                {887, 475, 8, 128, 189, 4011000},
                {1481, 991, 4, 128, 186, 4767000},
                {1481, 991, 8, 128, 186, 5157000},
                {1481, 991, 8, 256, 189, 5376000}};

到数据库的数据

dao_pc.get(key).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                ArrayList<Item_pc> item_pcs = new ArrayList<>();
                for (DataSnapshot data : snapshot.getChildren()) {
                    Item_pc item_pc = data.getValue(Item_pc.class);
                    item_pcs.add(item_pc);
                    key = data.getKey();
                }
                adapter_pc = new Adapter_pc(getApplicationContext(), item_pcs);
                recyclerView.setAdapter(adapter_pc);
                adapter_pc.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });

我的 Item_pc.class

public class Item_pc {
    private String cpu;
    private String gpu;
    private String ram;
    private String ssd;
    private String power;
    private String harga;

    public Item_score(String cpu, String gpu, String ram, String ssd, String power, String harga) {
        this.cpu = cpu;
        this.gpu = gpu;
        this.ram = ram;
        this.ssd = ssd;
        this.power = power;
        this.harga = harga;
    }

    public Item_pc(){}

    public String getCpu() {return cpu;}
    public void setCpu(String cpu) {this.cpu = cpu;}
    public String getGpu() {return gpu;}
    public void setGpu(String gpu) {this.gpu = gpu;}
    public String getRam() {return ram;}
    public void setRam(String ram) {this.ram = ram;}
    public String getSsd() {return ssd;}
    public void setSsd(String ssd) {this.ssd = ssd;}
    public String getPower() {return power;}
    public void setPower(String power) {this.power = power;}
    public String getHarga() {return harga;}
    public void setHarga(String harga) {this.harga = harga;}

条件:

  1. 我可以使用 arraylist 从 firebase 获取数据
  2. 我可以使用带有 TOPSIS 算法的虚拟数组计算数据

问题: 如何使用 TOPSIS 算法从 firebase 计算数据?我有麻烦来自firebase的数据需要使用Arraylist格式,我不知道如何在array[][]上存储数据,如java android上的array_push(PHP版本)。

【问题讨论】:

  • 请编辑您的问题并添加您的Item_score 课程的内容。
  • 您需要一个一个地处理列表中的每个元素,然后将数据添加到您的二维数组中。但这里显然存在一些类型转换问题。该列表被初始化为Integer 的列表,然后向其中添加Item_score 对象; Item_score 扩展 Integer 吗?似乎不太可能。然后你想将Item_score 中的int 值转换为double;为什么?
  • 好的,我更新了问题@codebod
  • 很抱歉,我看不到任何类声明。
  • 我很难理解。您的 Item_pc 类有 12 个字段,但您只需要一个包含 6 个字段的数组。

标签: java android arrays firebase firebase-realtime-database


【解决方案1】:

首先,不要将数字作为字符串存储在数据库或Item_pc 类中。例如,如果您将值存储为字符串,请将它们更改为 int。所以你的类声明应该是这样的:

class Item_pc {
    private int cpu;
    private int gpu;
    private int ram;
    private int ssd;
    private int power;
    private int harga;

    public Item_pc(int cpu, int gpu, int ram, int ssd, int power, int harga) {
        this.cpu = cpu;
        this.gpu = gpu;
        this.ram = ram;
        this.ssd = ssd;
        this.power = power;
        this.harga = harga;
    }

    public Item_pc() {
    }

    public int getCpu() {
        return cpu;
    }

    public void setCpu(int cpu) {
        this.cpu = cpu;
    }

    public int getGpu() {
        return gpu;
    }

    public void setGpu(int gpu) {
        this.gpu = gpu;
    }

    public int getRam() {
        return ram;
    }

    public void setRam(int ram) {
        this.ram = ram;
    }

    public int getSsd() {
        return ssd;
    }

    public void setSsd(int ssd) {
        this.ssd = ssd;
    }

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }

    public int getHarga() {
        return harga;
    }

    public void setHarga(int harga) {
        this.harga = harga;
    }
}

假设您从数据库中得到一个如下所示的列表:

ArrayList<Item_pc> item_pcs = new ArrayList<>();
item_pcs.add(new Item_pc(887, 475, 4, 128, 186, 3621000));
item_pcs.add(new Item_pc(887, 475, 8, 128, 189, 4011000));
item_pcs.add(new Item_pc(1481, 991, 4, 128, 186, 4767000));
item_pcs.add(new Item_pc(1481, 991, 8, 128, 186, 5157000));
item_pcs.add(new Item_pc(1481, 991, 8, 256, 189, 5376000));

要创建二维数组,请使用以下代码行:

int[][] data = new int[item_pcs.size()][6];
for (int i = 0; i < data.length; i ++) {
    Item_pc item_pc = item_pcs.get(i);
    data[i] = new int[]{
            item_pc.getCpu(),
            item_pc.getGpu(),
            item_pc.getRam(),
            item_pc.getSsd(),
            item_pc.getPower(),
            item_pc.getHarga()
    };
}
for (int[] d : data) {
    for (int i : d) {
        System.out.print(i + ", ");
    }
    System.out.println();
}

logcat 中的结果将是:

887, 475, 4, 128, 186, 3621000, 
887, 475, 8, 128, 189, 4011000, 
1481, 991, 4, 128, 186, 4767000, 
1481, 991, 8, 128, 186, 5157000, 
1481, 991, 8, 256, 189, 5376000,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多