Topic: 麻烦大家,一个LIST的问题

  Print this page

1.麻烦大家,一个LIST的问题 Copy to clipboard
Posted by: lionnick
Posted on: 2004-10-03 13:21

list.get()返回值是什么?
如果是arraylist.get(object),返回的是object的首地址吗?
怎样利用get()遍历LIST,然后再去调用object里的方法呢?

2.Re:麻烦大家,一个LIST的问题 [Re: lionnick] Copy to clipboard
Posted by: aleel_008
Posted on: 2004-10-03 16:10

你好象在C吧!java没有首地址的概念
for(Object i:list){}j就可以遍历

3.Re:麻烦大家,一个LIST的问题 [Re: lionnick] Copy to clipboard
Posted by: zerol
Posted on: 2004-10-03 19:32

In JDK1.4, we might use Iterator to iterate a list.



import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class TestList {

  public static void main(String[] args) {
    List lstMyList = new ArrayList();
    
    lstMyList.add("String1");
    lstMyList.add("String2");
    lstMyList.add("String3");
    lstMyList.add("String4");
    
    Iterator itr = lstMyList.iterator();
    while (itr.hasNext()) {
      String s = (String) itr.next();
      System.out.println(s.toUpperCase());
    }
    
  }
}

4.Re:麻烦大家,一个LIST的问题 [Re: lionnick] Copy to clipboard
Posted by: lionnick
Posted on: 2004-10-03 22:22

{ consider using code tag and "Disable Smileys" }

我是这样写的:

package Gerbil;
import java.util.*;

public class Gerbil {
private int gerbilNumber;
public Gerbil() {
gerbilNumber=(int )(Math.random()*10 );
}
public void hop(){
System.out.println("gerbilNumber="+gerbilNumber);
}
public static void main(String[] args) {
Gerbil g1=new Gerbil();
Gerbil g2=new Gerbil();
Gerbil g3=new Gerbil();
Gerbil g4=new Gerbil();
List gl=new ArrayList();
gl.add( g1 );
gl.add(g2);
gl.add(g3);
gl.add(g4);
for(int i=0;i<gl.size() ;i++)
{System.out.println((Gerbil) gl.get(i) );

}
}
}

输出的是:
Gerbil.Gerbil@126b249

Gerbil.Gerbil@182f0db

Gerbil.Gerbil@192d342

Gerbil.Gerbil@6b97fd
这是怎么回事?我想调用hop(),输出gerbilnumber,该怎么做?

5.Re:麻烦大家,一个LIST的问题 [Re: lionnick] Copy to clipboard
Posted by: why
Posted on: 2004-10-03 23:37

for(int i=0; i<gl.size(); i++) {
// System.out.println((Gerbil) gl.get(i)); <-- equivalnet to ((Gerbil) gl.get(i)).toString();
((Gerbil) gl.get(i)).hop();
}

6.Re:麻烦大家,一个LIST的问题 [Re: why] Copy to clipboard
Posted by: lionnick
Posted on: 2004-10-03 23:53

why wrote:
for(int i=0; i<gl.size(); i++) {
// System.out.println((Gerbil) gl.getLight Bulb); <-- equivalnet to ((Gerbil) gl.getLight Bulb).toString();
((Gerbil) gl.getLight Bulb).hop();
}



toString输出的那些字符是什么?

7.Re:麻烦大家,一个LIST的问题 [Re: lionnick] Copy to clipboard
Posted by: why
Posted on: 2004-10-03 23:59

lionnick wrote:
toString输出的那些字符是什么?

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#toString()

The toString method for class Object returns a string consisting of the
name of the class of which the object is an instance, the at-sign character
'@', and the unsigned hexadecimal representation of the hash code of
the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())

8.Re:麻烦大家,一个LIST的问题 [Re: lionnick] Copy to clipboard
Posted by: lionnick
Posted on: 2004-10-04 00:11

不愧是总斑竹,深夜还在论坛里.
小弟刚接触JAVA不久,问的问题十分粗浅,谢谢总斑竹孜孜不倦的指教!

9.Re:麻烦大家,一个LIST的问题 [Re: lionnick] Copy to clipboard
Posted by: why
Posted on: 2004-10-04 01:09

lionnick wrote:
不愧是总斑竹,深夜还在论坛里.
小弟刚接触JAVA不久,问的问题十分粗浅,谢谢总斑竹孜孜不倦的指教!

人在枫葉国,现在是下午。
要敝人在北京时间下午四时出现反而不行。Smile


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923