JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區 » 例外/錯誤處理  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 ArrayList的問題
temperfree





發文: 8
積分: 0
於 2010-03-30 11:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
程式碼為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
 
public class fibo{
    public static void main(String args[]){
      int n = 9;
      int sum=0;  
      ArrayList<Integer> al = new ArrayList<Integer>();
      for(int i=0;i<=n;i++){       
        if(i==0 || i==1) 
          al.set(i,1);
          System.out.print(1);
        sum = al.get(i-1) + al.get(i-2);        
        System.out.print(al);   
    }   
  }
}


--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.set(ArrayList.java:337)
at fibo.main(fibo.java:20)

Process completed.

執行完之後一直跑出上面的例外
可是又不太了解是什麼意思
請問各位大大 遇到這種情況是有那裡錯了嗎??
謝謝!!


reply to postreply to post
作者 Re:ArrayList的問題 [Re:temperfree]
mow





發文: 103
積分: 0
於 2010-03-30 12:02 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
temperfree wrote:
....恕刪

執行完之後一直跑出上面的例外
可是又不太了解是什麼意思
請問各位大大 遇到這種情況是有那裡錯了嗎??
謝謝!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
 
public class fibo{
    public static void main(String args[]){
      int n = 9;
      int sum=0;  
      ArrayList<Integer> al = new ArrayList<Integer>();
      for(int i=0;i<=n;i++){       
        if(i==0 || i==1) 
          al.set(i,1);                                    //這段寫法也有點問題(應該算地雷)
          System.out.print(1);
         sum = al.get(i-1) + al.get(i-2);        //這段邏輯有問題
        System.out.print(al);   
    }   
  }
}


mow edited on 2010-03-30 12:08
reply to postreply to post
作者 Re:ArrayList的問題 [Re:mow]
temperfree





發文: 8
積分: 0
於 2010-03-30 12:39 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
m大 可以請你說明白一點嗎?
包括你說的地雷跟邏輯的地方
小弟不是很明白~
麻煩你了 感恩^^


reply to postreply to post
作者 Re:ArrayList的問題 [Re:temperfree]
mow





發文: 103
積分: 0
於 2010-03-30 13:22 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
當 i = 0 ,
會執行 al.set(i,1);
接下來sum = al.get(i-1) + al.get(i-2); => sum = al.get(-1) + al.get(-2);
結果會是?

至於地雷呢
請看ArrayList的API
裡面的:

建構子
ArrayList()
方法
add(E e)
set(int index, E element)
ensureCapacity(int minCapacity)


reply to postreply to post
作者 Re:ArrayList的問題 [Re:mow]
temperfree





發文: 8
積分: 0
於 2010-03-30 13:35 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝
我再研究看看@@
感恩哦^^


reply to postreply to post
作者 Re:ArrayList的問題 [Re:mow]
LiaoLuke





發文: 106
積分: 0
於 2010-03-31 10:06 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
mow wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
 
public class fibo{
    public static void main(String args[]){
      int n = 9;
      int sum=0;  
      ArrayList<Integer> al = new ArrayList<Integer>();
      for(int i=0;i<=n;i++){       
        if(i==0 || i==1) 
          al.set(i,1);                                    //這段寫法也有點問題(應該算地雷)
          System.out.print(1);
         sum = al.get(i-1) + al.get(i-2);        //這段邏輯有問題
        System.out.print(al);   
    }   
  }
}



sum = al.get(i-1) + al.get(i-2);
因為:
當 i = 0 時,
al.get(-1) >>>???
al.get(-2) >>>???

所以
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0


reply to postreply to post
» JWorld@TW »  Java 新手區 » 例外/錯誤處理

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8