0

I already have TOPSIS algorithm that can calculate data from an array[][]. Now I'm going to use the data from:

dummy data:

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

to database's data

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

My 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;}

Condition:

  1. I can get data from firebase using arraylist
  2. I can calculate data using dummy array with TOPSIS algorithm

problem : how to calculate data from firebase with TOPSIS algorithm? I have trouble that data from firebase need to use Arraylist format, I don't know how to store data on array[][] like array_push(PHP version) on java android.

9
  • Please edit your question and add the content of your Item_score class. Commented Jul 2, 2021 at 7:48
  • You will need to process each element of the list one by one and then add the data to your 2d array. But there's clearly some type conversion issues here. The list is initialized as a list of Integer and then you add Item_score objects to it; does Item_score extend Integer? Seems unlikely. And then you want to cast the int values within Item_score to double; why? Commented Jul 2, 2021 at 7:52
  • Okay, ive updated the question @codebod Commented Jul 2, 2021 at 8:13
  • I'm sorry but I cannot see any class declaration. Commented Jul 2, 2021 at 8:34
  • I have a hard time understanding. Your Item_pc class has 12 fields but you only want an array with 6 fields. Commented Jul 2, 2021 at 9:30

1 Answer 1

2

First of all, do not store numbers as Strings in the database nor in your Item_pc class. If you have the values stored as Strings, change them to int, for example. So your class declaration should look like this:

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

Assuming that you get from the database a list that looks like this:

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

To create the two-dimensional array, please use the following lines of code:

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

The result in the logcat will be:

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,
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.