当前位置: 技术问答>java相关
一个很弱的问题。关于返回一个列表
来源: 互联网 发布时间:2015-07-08
本文导语: 请问:在Java中返回一个如下的列表都有什么方法 最好能给出代码。谢谢! ID Name Des 1 A A1 2 B B2 3 C C3 | 使用Vector + Hashtable import java.util.*; public class Test { public Vector test() {...
请问:在Java中返回一个如下的列表都有什么方法
最好能给出代码。谢谢!
ID Name Des
1 A A1
2 B B2
3 C C3
最好能给出代码。谢谢!
ID Name Des
1 A A1
2 B B2
3 C C3
|
使用Vector + Hashtable
import java.util.*;
public class Test
{
public Vector test()
{
Vector v = new Vector();
Hashtable h1 = new Hashtable();
h1.put("ID", "1");
h1.put("Name", "A");
h1.put("Des", "A1");
v.add(h1);
Hashtable h2 = new Hashtable();
h2.put("ID", "2");
h2.put("Name", "B");
h2.put("Des", "B2");
v.add(h2);
....
return v;
}
public void list()
{
Iterator itr = test().iterator();
while(itr.hasNext())
{
Hashtable h = (Hashtable) itr.next();
System.out.println("ID: " + h.get("ID").toString());
System.out.println("Name: " + h.get("Name").toString());
System.out.println("Des: " + h.get("Des").toString());
}
}
}
import java.util.*;
public class Test
{
public Vector test()
{
Vector v = new Vector();
Hashtable h1 = new Hashtable();
h1.put("ID", "1");
h1.put("Name", "A");
h1.put("Des", "A1");
v.add(h1);
Hashtable h2 = new Hashtable();
h2.put("ID", "2");
h2.put("Name", "B");
h2.put("Des", "B2");
v.add(h2);
....
return v;
}
public void list()
{
Iterator itr = test().iterator();
while(itr.hasNext())
{
Hashtable h = (Hashtable) itr.next();
System.out.println("ID: " + h.get("ID").toString());
System.out.println("Name: " + h.get("Name").toString());
System.out.println("Des: " + h.get("Des").toString());
}
}
}
|
你是想得到一个JTable么?
这个n简单啊……
class:
import javax.swing.*;
public class GetTable {
public GetTable() {
Object[][] cells = {
{
"1","A","A1"
},
{
"2","B","B2"
},
{
"3","C","C3"
}
};
String[] columnNames = {"ID","Name"," Des"};
table = new JTable(cells,columnNames);
}
public JTable getTable() {
return table;
}
private JTable table;
}
调用:
GetTable gt = new GetTable();
JTable table = gt.getTable();
这个n简单啊……
class:
import javax.swing.*;
public class GetTable {
public GetTable() {
Object[][] cells = {
{
"1","A","A1"
},
{
"2","B","B2"
},
{
"3","C","C3"
}
};
String[] columnNames = {"ID","Name"," Des"};
table = new JTable(cells,columnNames);
}
public JTable getTable() {
return table;
}
private JTable table;
}
调用:
GetTable gt = new GetTable();
JTable table = gt.getTable();