java如何表示数据结构

java的链表如何表示?
我知道c++可以用指针.
struct p
{
int data;
p *next
};
但是java 呢?

一、List接口,有序的Collection接口,精确地控制每个元素插入的位置,允许有相同的元素
1.链表,LinkedList实现了List接口,允许null元素,提供了get()、remove()、insert()方法。
[java] view plaincopy
public void add() {
LinkedList List = new LinkedList();
List.add("link1");
List.add("link2");
List.add("link3");
Iterator it = List.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
it.remove();
Iterator it1 = List.iterator();
for (int i = 0; i < List.size(); i++) {
System.out.println(it1.next());
}
}

2.数组列表,ArrayList,可以动态变化容量的数组,数组列表中存放Object类型,在数组列表中存放的对象类型,以其原型的父类代替,提取其中的元素时要进行类型转换
[java] view plaincopy
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add("name");
al.add("value");
al.add("number");
for(int i=0;i<al.size();i++)
{
System.out.println(al.get(i));
}
}

二、Set接口,不包含重复元素的Collection接口
1.散列集,HashSet,实现了Set接口,非线性同步与链表和数组列表几乎类似,处理时链表进行数据处理花费时间更短,处理大数据时通常使用散列集
[java] view plaincopy
public static void main(String[] args)
{
long time=0;
HashSet hs=new HashSet();
ArrayList al=new ArrayList();
long starttime=System.currentTimeMillis();
for(int i=0;i<10000;i++)
{
hs.add(new Integer(i));
}
System.out.println(System.currentTimeMillis()-starttime);
for(int i=0;i<10000;i++)
{
al.add(new Integer(i));
}
System.out.println(System.currentTimeMillis()-starttime);
}

2.树集,TreeSet,实现了Set接口,实现了排序功能,集合中的元素默认按升序排列元素。
三、Map接口,没有继承Collection接口,其提供key到value的映射,Map中不能包含相同的key,每个key只能映射一个value。
1.散列表类,HashTable,继承了Map接口,非空(non-null)的对象都可作为key或value,特点:无序的可以快速查找特定的元素

[java] view plaincopy
public static void TableTest(){
Hashtable ht = new Hashtable();
ht.put("key1", "value1");
ht.put("key2", "value2");
String value1=(String)ht.get("key2");
System.out.println(value1);
}

2.散列映射类,HashMap,与HashTable类似,HashMap是非同步的,且允许null

[java] view plaincopy
public static void Maptest(){
Map<string string=""> map=new HashMap<string string="">();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
for(Map.Entry<string string=""> entry:map.entrySet()){
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
String value1=(String)map.get("key1");
System.out.println(value1);
}
</string></string></string>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-11-04
JAVA中没有指针,它的链表可以用List及其子类来实现.
第2个回答  2009-11-04
java有直接的链表和队列,ListArray吧。
第3个回答  2009-11-04
public class StructTester {

public static void main(String args[]) {
Node head = new Node() ;
head.data = 1;

Node ne = new Node() ;
ne.data = 2;

head.next = ne ;

Node cur = head ;
while(cur != null) {
System.out.println (cur.data) ;
cur = cur.next ;
}
}
}

class Node {

public int data ;

public Node next ;
}
java中没有指针,只有引用。上面的程序是一个单向链表的简单实现。 至于你说的java中的链表,java已经提供相应的API,java.util.LinkedList就是一个实现好的单向链表。本回答被提问者采纳
第4个回答  2009-11-04
告诉你java用内部类表示节点。
如果提高悬赏我会给你代码。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网