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.é¾è¡¨ï¼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就是一个实现好的单向链表。本回答被提问者采纳
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用内部类表示节点。
如果提高悬赏我会给你代码。
如果提高悬赏我会给你代码。