类似于java数据结构的表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1691664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Table like java data structure
提问by Sverd
I need to implement some kind table-like data structure that stores info like this in Java:
我需要实现某种类似表的数据结构,在 Java 中存储这样的信息:
+--------+-------+-----+
| sij | i | j |
+--------+-------+-----+
| 45 | 5 | 7 |
+--------+-------+-----+
| 33 | 1 | 6 |
+--------+-------+-----+
| 31 | 0 | 9 |
+--------+-------+-----+
| 12 | 8 | 2 |
+--------+-------+-----+
and I have to be able to sort the table by the sij
parameter. I've made some tests with ArrayList
and HashMap
, but I can't make them work well.
我必须能够按sij
参数对表格进行排序。我已经用ArrayList
和做了一些测试HashMap
,但我不能让它们正常工作。
回答by OscarRyz
What do you mean with:
你是什么意思:
i have to be able to sort it by the sij parameter
我必须能够通过 sij 参数对其进行排序
What's wrong with:
有什么问题:
Object [][] data
EDIT
编辑
Ok, just guessing that what you need is a "StrangeDataStructure"which holds the array, and helps you to sort by the first column, then the only thing that you need is something like this:
好的,只是猜测您需要的是一个“StrangeDataStructure”,它包含数组,并帮助您按第一列排序,那么您唯一需要的是这样的:
class Structure {
Object [][] data;
Object [] indexColumn; // the sij?
}
And that's it: you should add a sort method indicating the direction, and sort using the "indexColumn"
就是这样:您应该添加一个指示方向的排序方法,并使用“indexColumn”进行排序
It is VEEERY simple I think ( and If I understood your "question")
我认为这非常简单(如果我理解你的“问题”)
You know what? I'm going to implement it.
你知道吗?我要实施它。
// time elapses...
// 时间过去了...
Here it is:
这里是:
import java.util.Comparator;
import java.util.Arrays;
public class StrangeStructure {
private Integer [][] data;
private Integer [] sij; // what is sij anyway?
public StrangeStructure( Integer [][] matrix ) {
data = matrix;
sij = new Integer[ data.length ];
for( int i = 0 ; i < data.length ; i++ ) {
sij[i] = data[i][0];
}
}
public void sort( Direction direction ) {
Comparator sijComparator = new DataComparator( direction, true );
Comparator dataComparator = new DataComparator( direction, false );
Arrays.sort( sij, sijComparator );
Arrays.sort( data, dataComparator );
}
public static void main( String [] args ) {
StrangeStructure s =
new StrangeStructure( new Integer[][]{
{ 45, 5, 7 },
{ 33, 1, 6 },
{ 31, 0, 9 },
{ 12, 8, 2 }
});
System.out.printf("Original:\n%s", s );
s.sort( Direction.MIN_TO_MAX );
System.out.printf("Min to max:\n%s", s );
s.sort( Direction.MAX_TO_MIN );
System.out.printf("Max to min\n%s", s );
}
public String toString() {
StringBuilder b = new StringBuilder();
for( Integer [] row : data ) {
for( int i : row ) {
b.append( i+",");
}
b.append("\n");
}
return b.toString();
}
}
class DataComparator implements Comparator {
private Direction direction;
private boolean isSij;
public DataComparator( Direction d, boolean isSij ) {
this.direction = d;
this.isSij = isSij;
}
public int compare( Object one , Object two ) {
if( isSij ){
return doCompare( direction, (Integer) one, (Integer) two );
} else {
return doCompare( direction, ((Integer[])one)[0], ((Integer[])two)[0]);
}
}
public int doCompare( Direction d, int one, int two ) {
int a = ( d == Direction.MIN_TO_MAX? one: two );
int b = ( d == Direction.MIN_TO_MAX? two: one ) ;
return a - b;
}
public boolean equals( Object o ) {
return false;
}
}
enum Direction{
MIN_TO_MAX,
MAX_TO_MIN
}
Output:
输出:
Original:
45,5,7,
33,1,6,
31,0,9,
12,8,2,
Min to max:
12,8,2,
31,0,9,
33,1,6,
45,5,7,
Max to min
45,5,7,
33,1,6,
31,0,9,
12,8,2,
回答by camickr
Read the section from the Swing tutorial on How to Use Tables. The tutorial shows how to create a table as well as how to add sorting capability to the table.
阅读 Swing 教程中关于如何使用表的部分。本教程展示了如何创建表格以及如何向表格添加排序功能。
If you only need to store the data but not display it, then you can use a 2-dimensional array or a List of Lists. Then you can use the Column Comparatorto do the sorting.
如果您只需要存储数据而不显示数据,那么您可以使用二维数组或列表列表。然后您可以使用列比较器进行排序。
Edit: added code demonstrating use of the ColumnComparator
编辑:添加了演示 ColumnComparator 使用的代码
import java.util.*;
public class SortSIJ
{
public static void main(String args[])
{
Object[] data = new Object[4];
data[0] = new Integer[] {45, 5, 7};
data[1] = new Integer[] {33, 1, 6};
data[2] = new Integer[] {31, 0, 9};
data[3] = new Integer[] {12, 8, 2};
ColumnComparator cc = new ColumnComparator(0);
// cc.setAscending( false );
Arrays.sort(data, cc);
for (Object row: data)
{
Integer[] theRow = (Integer[])row;
System.out.println( Arrays.asList(theRow) );
}
}
}
I also agree with the suggestion to create an Object to store the 3 variables. In this case you can use the BeanComparatorwhich can be found at the above link.
我也同意创建一个对象来存储 3 个变量的建议。在这种情况下,您可以使用可在上述链接中找到的BeanComparator。
回答by camickr
One option is to create a new object that contains the 3 variables, and then make an array/tree of those objects, and sort by the parameter you want.
一种选择是创建一个包含 3 个变量的新对象,然后创建这些对象的数组/树,并按所需参数排序。
回答by Jim Ferrans
Here's one way: make an object called Row to hold each row, and then make a java.util.HashMap whose keys are Integer sij's and whose values are the corresponding Rows.
这是一种方法:创建一个名为 Row 的对象来保存每一行,然后创建一个 java.util.HashMap,其键是 Integer sij 的,其值是相应的 Row。
public class Example
{
public static class Row
{
public Integer sij;
public Integer i;
public Integer j;
public Row(Integer sij, Integer i, Integer j)
{
this.sij = sij;
this.i = i;
this.j = j;
}
}
public static void main(String[] args)
{
Row r1 = new Row(45, 5, 7);
Row r2 = new Row(33, 1, 6);
Row r3 = new Row(31, 0, 9);
Row r4 = new Row(12, 8, 2);
Map<Integer, Row> map = new TreeMap<Integer, Row>();
map.put(r1.sij, r1);
map.put(r2.sij, r2);
map.put(r3.sij, r3);
map.put(r4.sij, r4);
for ( Row row : map.values() ) {
System.out.println("sij: " + row.sij + " i: " + row.i + " j: " + row.j);
}
}
}
When this runs it produces:
当它运行时,它会产生:
sij: 12 i: 8 j: 2
sij: 31 i: 0 j: 9
sij: 33 i: 1 j: 6
sij: 45 i: 5 j: 7
回答by Atmocreations
You could use the MultiValueMapfrom Apache in order to link multiple values with one key.
您可以使用Apache的MultiValueMap以将多个值与一个键链接起来。
回答by finnw
If I understand your question right, all you need is a Comparable
class to represent a row.
如果我理解你的问题是正确的,你所需要的只是一个Comparable
代表一行的类。
public static class Row
implements Comparable<Row> {
public Row(int sij, int i, int j) {
this.sij = sij;
this.i = i;
this.j = j;
}
public int compareTo(Row other) {
return Integer.valueOf(sij).compareTo(other.sij);
}
public final int sij;
public final int i;
public final int j;
}
You can then populate a List
with instances of Row
and use Collections.sort
to sort it.
然后,您可以List
使用 的实例填充 aRow
并用于对其Collections.sort
进行排序。
回答by Andrejs
There is a generic TreeBasedTable
class from Google guavalibrary which does exactly what you are asking for. It also offers many other useful utility methods and its usage is shown in the user guide.
TreeBasedTable
谷歌番石榴库中有一个通用类,它完全符合您的要求。它还提供了许多其他有用的实用方法,其用法在用户指南中有所说明。
From the TreeBasedTable
docs:
从TreeBasedTable
文档:
Implementation of Table whose row keys and column keys are ordered by their natural ordering or by supplied comparators.
Table 的实现,其行键和列键按其自然顺序或提供的比较器排序。
Example usage:
用法示例:
RowSortedTable<Vertex, Vertex, Double> weightedGraph = TreeBasedTable.create();
weightedGraph.put(v2, v3, 4.0);
weightedGraph.put(v1, v2, 20.0);
System.out.println( weightedGraph.rowKeySet() ); // prints [v1, v2]