|
-
Converting arraylist to array
Hi,
Im new to java and would appreciate some help.
I have declared the following ArrayList moves = new ArrayList();
I have then populated the arraylist and would lik to make it availble as an array now.
How can I do this?
I have tried this but gives error
int[] moves2 = moves.ToArray() ;
This is my completet code, its trying to find legal moves then put them all into and array and return them. If you see anything else about my code that needs adjusting, please tell.
public int[] find_moves(){
ArrayList moves = new ArrayList();
for(int i=0; i<=31; i++){
System.out.println("board["+i+"] = "+board[i]);}
for(int i=1; i<=4; i++){
if (board[i]==1 && board[i+5]==0){
moves.add(+i+" "+ (i+5)); //System.out.println((i+1)+ "-"+ (i+6));
}
if (board[i]==1 && board[i+4]==0){
moves.add(+i+ "-"+ (i+4));}
}
for(int i=5; i<=8; i++){
if (board[i]==1 && board[i+4]==0){
moves.add(+i+ "-"+ (i+4));
}
if (board[i]==1 && board[i+3]==0){
moves.add(+i+ "-"+ (i+3));}
}
for(int i=9; i<=12; i++){
if (board[i]==1 && board[i+5]==0){
moves.add(+i+ "-"+ (i+5));
}
if (board[i]==1 && board[i+4]==0){
moves.add(+i+ "-"+ (i+4));}
}
for(int i=13; i<=16; i++){
if (board[i]==1 && board[i+4]==0){
moves.add(+i+ "-"+ (i+4));
}
if (board[i]==1 && board[i+3]==0){
moves.add(+i+ "-"+ (i+3));}
}
for(int i=17; i<=20; i++){
if (board[i]==1 && board[i+5]==0){
moves.add(+i+ "-"+ (i+5));
}
if (board[i]==1 && board[i+4]==0){
moves.add(+i+ "-"+ (i+4));}
}
for(int i=21; i<=24; i++){
if (board[i]==1 && board[i+4]==0){
moves.add(+i+ "-"+ (i+4));
}
if (board[i]==1 && board[i+3]==0){
moves.add(+i+ "-"+ (i+3));}
}
System.out.println(moves);
int[] moves2 = moves.ToArray() ;
return null;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks