如何将算法的输出存储到新数组中

时间:2021-07-13 14:02:20
value1 = 0 and value2 = 0 with weight 0

value1 = 1 and value2 = 6 with weight 5

value1 = 2 and value2 = 4 with weight 4

value1 = 3 and value2 = 3 with weight 2

value1 = 5 and value2 = 2 with weight 4

value1 = 5 and value2 = 7 with weight 6

value1 = 8 and value2 = 5 with weight 6

How will i store this output into a new array (different array for value1, value2 and weight) and compare them

我将如何将此输出存储到新数组(value1,value2和weight的不同数组)并进行比较

3 个解决方案

#1


0  

You can create a bean containing the value1, value2 and weight like this

您可以像这样创建一个包含value1,value2和weight的bean

public class AlgoItem {
    private Integer value1;
    private Integer value2;
    private Integer weight;
    // Setter and getter
}

And use a Collection to store it (you can use a HashSet or a ArrayList for implementation of a Collection)

并使用Collection来存储它(您可以使用HashSet或ArrayList来实现Collection)

So your method will return a Collection of AlgoItem

所以你的方法将返回一个AlgoItem集合

#2


0  

You can use a two dimensional array

您可以使用二维数组

        int[][] values = new int[][] {{ 0, 0, 0}, {1, 6, 5}, {2,4,4}};

The first array is an array of arrays containing your 3 values.

第一个数组是包含3个值的数组数组。

This is the simple and dirty way. I would recommend making an Object which holds all 3 of your values and then making a list of them.

这是简单而肮脏的方式。我建议创建一个包含所有3个值的Object,然后列出它们。

#3


0  

I'm still not sure what you're problem is. Perhaps you should show us what you've tried that didn't work.

我还不确定你的问题是什么。也许你应该向我们展示你尝试过的不起作用的东西。

int[] value1=new int[7];
int[] value2=new int[7];
int[] weight=new int[7];

value1[0]=0
value2[0]=0;
weight[0]=0;

value1[1]=1;
value2[1]=6;
weight[1]=5;

value1[2]=2;
value2[2]=4;
weight[2]=4;

... etc ...

Is that what you're looking for?

这就是你要找的东西吗?

#1


0  

You can create a bean containing the value1, value2 and weight like this

您可以像这样创建一个包含value1,value2和weight的bean

public class AlgoItem {
    private Integer value1;
    private Integer value2;
    private Integer weight;
    // Setter and getter
}

And use a Collection to store it (you can use a HashSet or a ArrayList for implementation of a Collection)

并使用Collection来存储它(您可以使用HashSet或ArrayList来实现Collection)

So your method will return a Collection of AlgoItem

所以你的方法将返回一个AlgoItem集合

#2


0  

You can use a two dimensional array

您可以使用二维数组

        int[][] values = new int[][] {{ 0, 0, 0}, {1, 6, 5}, {2,4,4}};

The first array is an array of arrays containing your 3 values.

第一个数组是包含3个值的数组数组。

This is the simple and dirty way. I would recommend making an Object which holds all 3 of your values and then making a list of them.

这是简单而肮脏的方式。我建议创建一个包含所有3个值的Object,然后列出它们。

#3


0  

I'm still not sure what you're problem is. Perhaps you should show us what you've tried that didn't work.

我还不确定你的问题是什么。也许你应该向我们展示你尝试过的不起作用的东西。

int[] value1=new int[7];
int[] value2=new int[7];
int[] weight=new int[7];

value1[0]=0
value2[0]=0;
weight[0]=0;

value1[1]=1;
value2[1]=6;
weight[1]=5;

value1[2]=2;
value2[2]=4;
weight[2]=4;

... etc ...

Is that what you're looking for?

这就是你要找的东西吗?