Converting arraylist to array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Converting arraylist to array

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    7

    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;
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Make a Move class

    You should make yourself a Move class like:

    Code:
    class Move {
      private int position;
      private int value;
      public Move(int position, int value) {
        this.position=position;
        this.value=value;
      }
      public int getPosition() {
        return position;
      }
      public int getValue() {
        return value;
      }
      public String toString() {
        return (position+" "+value);
      }
    }
    and add a move to the list not like:

    moves.add(+i+" "+ (i+5));

    but like:

    moves.add(new Move(i,i+5));
    ------
    When you code like:

    int[] moves2 = moves.ToArray() ;

    you get an error because the toArray() method returns an array of Objects,
    not integers.

    You are adding Strings to the list so the correct would be to typecast like:

    String [] moves2=(String[])moves.toArray();

    but a String array is not a good idea because then you would have to
    manipulate each string in the array when you want to get the info. Thats
    where the Move class comes in...
    -----

    In other words, your public int[] find_moves() method should be written
    like:

    public Move[] find_moves()

    and the last statement in this method should be:

    return (Move [])moves.toArray() ; (not null ... )

    That method could then be used like:

    Move [] moveArray=find_moves();
    Move aMove=moveArray[2];
    System.out.println(aMove.toString());
    int aPosition=aMove.getPosition();
    int aValue=aMove.getValue();
    Last edited by sjalle; 03-17-2005 at 07:34 AM.
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    7

    The objects?

    Hi,

    Thanks for the comment, I found it very useful. This is how I have changed it but Im unsure of your last part

    Move [] moveArray=find_moves();
    Move aMove=moveArray[2];
    System.out.println(aMove.toString());
    int aPosition=aMove.getPosition();
    int aValue=aMove.getValue();

    Im trying to get my head around what is actually happening here. Its fine when Im using int etc, but as soon as it looks like Move[] aMove, I get really confused.

    Here is my code but it doesnt print the moveArray?


    public class array_test{


    public static void main (String[] args) {
    Move[] moveArray=find_moves();
    System.out.println(moveArray);}


    int board[] = {0,1, 2, 3, 4, 5 ,6 , 7, 8, 9, 10, 11, 12, 13, 14, 15,1,
    17, 18, 0, 0, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

    public Move[] find_moves(){

    ArrayList movesArrayList = 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){
    movesArrayList.add(new Move(i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    }

    for(int i=5; i<=8; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }

    for(int i=9; i<=12; i++){
    if (board[i]==1 && board[i+5]==0){
    movesArrayList.add(new Move (i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+5));}
    }

    for(int i=13; i<=16; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }
    for(int i=17; i<=20; i++){
    if (board[i]==1 && board[i+5]==0){
    movesArrayList.add(new Move (i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    }

    for(int i=21; i<=24; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }

    System.out.println(movesArrayList);

    return (Move [])movesArrayList.toArray() ;

    }
    }



    class Move {
    private int position;
    private int value;
    public Move(int position, int value) {
    this.position=position;
    this.value=value;
    }
    public int getPosition() {
    return position;
    }
    public int getValue() {
    return value;
    }
    public String toString() {
    return (position+" "+value);
    }
    }

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Ok:

    // call find_moves, the method returns the pointer to an array of Move
    // object pointers so the variable we store that pointer in has to be declared as
    // an array of Move object pointers:
    Move [] moveArray=find_moves();
    // we select one of the Move object pointers from that array (index 2 in this case)
    // just to illustrate...
    Move aMove=moveArray[2];
    // we peint out thanl Move object using its toString() method
    System.out.println(aMove.toString());
    // we get the position of that Move object
    int aPosition=aMove.getPosition();
    // we get the value of that Move object
    int aValue=aMove.getValue();

    Printing the Move array out:

    A java array has no automatic handling that gives you a nice list of its
    its elements, so if you code:

    System.out.println(moveArray);

    you will only get the value of the address (pointer) to that array printed out.

    You will have to do it in a for loop:
    Code:
     
    for (int k=0; i<moveArray.length; k++) {
      System.out.println(moveArray[k]);
    }
    here:
    Code:
    moveArray[k]
    is the pointer to the Move object at index k, and since
    the toString method is defined for the Move class the
    System.out.println(moveArray[k]); will use the toString method of that
    Move object. This is because the System.out.println method always calls
    an objects toString method. If there are no such method the
    System.out.println will use the toString method of java.lang.Object, which is
    the ancestor of all java classes, wether they like it or not , and the
    toString methof of java.lang.Object return a string representation of the
    object's address.

    PS: Move[] aMove would confuse me too, as i would hesitate to name an
    array consisting of Move objects aMove....


    PSPS: I must stop using i as index in my examples, it just makes everything italic
    Last edited by sjalle; 03-17-2005 at 09:35 AM.
    eschew obfuscation

  5. #5
    Join Date
    Mar 2005
    Posts
    7
    I get no such main error when I run this.....

    Also what would be the difference if the move class was placed as it is below, or as it is before..outside the other class.

    Thanks very much for the help, its soaking in




    public class array_test{



    public void main (String[] args) {
    Move [] moveArray=pointer_to_moves();
    Move aMove=moveArray[2];
    System.out.println(aMove.toString());
    int aPosition=aMove.getPosition();
    int aValue=aMove.getValue();

    }


    int board[] = {0,1, 2, 3, 4, 5 ,6 , 7, 8, 9, 10, 11, 12, 13, 14, 15,1,
    17, 18, 0, 0, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

    public Move[] pointer_to_moves(){

    ArrayList movesArrayList = 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){
    movesArrayList.add(new Move(i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    }

    for(int i=5; i<=8; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }

    for(int i=9; i<=12; i++){
    if (board[i]==1 && board[i+5]==0){
    movesArrayList.add(new Move (i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+5));}
    }

    for(int i=13; i<=16; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }
    for(int i=17; i<=20; i++){
    if (board[i]==1 && board[i+5]==0){
    movesArrayList.add(new Move (i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    }

    for(int i=21; i<=24; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }

    System.out.println(movesArrayList);

    return (Move [])movesArrayList.toArray() ;

    }

    class Move {
    private int position;
    private int value;
    public Move(int position, int value) {
    this.position=position;
    this.value=value;
    }
    public int getPosition() {
    return position;
    }
    public int getValue() {
    return value;
    }
    public String toString() {
    return (position+" "+value);
    }
    }

    }

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    If the Move class is in the same file as the array_test, and the java file is
    named array_test.java it cannot be declared as public,
    unless you place the public static void main(String [] args) inside the Move class' body.
    But: the way you have done it, Move is an inner class of array_test, like:
    Code:
    public class AClass {
      .
      public static void main(String [] args) {
        .
        .
      }
      .
      classBClass { // BClass is an inner class of AClass
         .
         .
      }
    }
    so you cannot declare BClass before AClass if it is going to be an inner class
    of AClass ...
    You could have done it like this in the file AClass.java:

    Code:
    class BClass {
      .
      .
    } 
    public class AClass {
      .
      public static void main(String [] args) {
        .
        .
      }
      .
    }
    or like this in the file BClass.java:

    Code:
    public class BClass {
      .
      public static void main(String [] args) {
        .
        .
      }
      .
    } 
    class AClass {
      .
      .
    }
    In the two examples above the sequence of AClass and BClass is irrelevant,
    but for flexibility you should do like this:

    ...the file AClass.java:
    Code:
    public class AClass {
      .
      public static void main(String [] args) {
        .
        .
      }
      .
    }
    ...the file BClass.java:
    Code:
    public class BClass {
      .
      .
    }
    The rule is that the class that gives the java file its name must be (the only one)
    declared as public in that file.
    Last edited by sjalle; 03-17-2005 at 12:53 PM.
    eschew obfuscation

  7. #7
    Join Date
    Mar 2005
    Posts
    7
    Hi,

    Thanks for the tips, I have used it all and your help has been tremendous. There is one thing that is still puzzling me.

    Im trying to create the original array again thats going to contain a list of moves eg
    (2-7, 1-6) (moves can be more than one eg checkers)and most the time i get non static method find moves cannot be referenced from static context.

    This is how I picture I need to create this array.

    Firsrt create an instance of class array test.
    array_test mytest= new array_test();

    This creates an instance of the class which contains an array board which is called using mytest.board

    Then I would like to apply the method find_moves on the mytest.board which should return a pointer to the array.

    This is where it starts getting hazy......

  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    When you do it from the public static void main(String [] args) method
    you are in effect "outside" the array_test class.
    You have to invoke the method find_moves via the object pointer of the
    array_test instance, mytest, that you have already created, like:

    Code:
    array_test mytest= new array_test();
    Move[] moves=mytest.find_moves();
    eschew obfuscation

  9. #9
    Join Date
    Mar 2005
    Posts
    7
    Thats exactly what I thought, however when i try do that it, there is no problem with compliling, but when run it throws an exception error.

    This is the code so far, this program will be interfacing with a refree class and implementing a getMove method which must consist of an array of moves.

    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.*;
    import java.util.StringTokenizer;
    import java.util.ArrayList;
    import java.rmi.*;
    import java.net.*;




    class Move {

    private int position;
    private int value;
    public Move(int position, int value) {
    this.position=position;
    this.value=value;
    }
    public int getPosition() {
    return position;
    }
    public int getValue() {
    return value;
    }
    public String toString() {
    return (position+" "+value);
    }
    }



    public class array_test{


    public static void main (String[] args) {
    array_test mytest= new array_test();
    Move[] moves=mytest.find_moves();
    }


    int board[]= {0,1, 2, 5, 4, 5 ,6 , 7, 8, 9, 10, 11, 12, 13, 14, 15,1,
    17, 18, 0, 0, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};


    public array_test (){
    };


    public Move[] find_moves(){


    ArrayList movesArrayList = 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){
    movesArrayList.add(new Move(i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    }

    for(int i=5; i<=8; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }

    for(int i=9; i<=12; i++){
    if (board[i]==1 && board[i+5]==0){
    movesArrayList.add(new Move (i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+5));}
    }

    for(int i=13; i<=16; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }
    for(int i=17; i<=20; i++){
    if (board[i]==1 && board[i+5]==0){
    movesArrayList.add(new Move (i,i+5));}
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    }

    for(int i=21; i<=24; i++){
    if (board[i]==1 && board[i+4]==0){
    movesArrayList.add(new Move (i,i+4));}
    if (board[i]==1 && board[i+3]==0){
    movesArrayList.add(new Move (i,i+3));}
    }

    //System.out.println(movesArrayList);

    return (Move [])movesArrayList.toArray() ;

    }



    }

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Wow

    I have never seen that one ...????????? Also, I have always thought that
    typecasting pointers from Object arrays to other arrays was plain stuff. I
    am really confused now, and the exception you got looks like a Java VM
    bug
    ....

    Anyway, I have kicked the code around and it behaves well now....(knock on wood)..

    Btw: why do the toArray() to get an array ?, why not pass back the arraylist
    itself and forget the Move[] moves array ?

    Code:
    import java.util.*;
    
    class Move {
    
    
        private int position;
        private int value;
        public Move(int position, int value) {
          this.position = position;
          this.value = value;
        }
        public Move() {
        }
        public void setValues(int position, int value) {
          this.position = position;
          this.value = value;
        }
        public int getPosition() {
          return position;
        }
    
        public int getValue() {
          return value;
        }
    
        public String toString() {
          return (position + " " + value);
        }
      }
    
    public class ArrayTest {
    
      public static void main(String[] args) {
        ArrayTest mytest = new ArrayTest();
        mytest.doIt();
      }
      public void doIt() {
        Object [] ob = find_moves();
        for (int i=0;i<ob.length;i++) {
          Move aMove=(Move)ob[i];
          System.out.println(aMove);
        }
    
      }
      int board[] = {
          0, 1, 2, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1,
          17, 18, 0, 0, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
    
      public ArrayTest() {
      }
    
      public Object [] find_moves() {
    
        ArrayList movesArrayList = 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) {
            makeMove(movesArrayList,i, i + 5);
          }
          if (board[i] == 1 && board[i + 4] == 0) {
            makeMove(movesArrayList,i, i + 4);
          }
        }
    
        for (int i = 5; i <= 8; i++) {
          if (board[i] == 1 && board[i + 4] == 0) {
            makeMove(movesArrayList,i, i + 4);
          }
          if (board[i] == 1 && board[i + 3] == 0) {
            makeMove(movesArrayList,i, i + 3);
          }
        }
    
        for (int i = 9; i <= 12; i++) {
          if (board[i] == 1 && board[i + 5] == 0) {
            makeMove(movesArrayList,i, i + 5);
          }
          if (board[i] == 1 && board[i + 4] == 0) {
            makeMove(movesArrayList,i, i + 5);
          }
        }
    
        for (int i = 13; i <= 16; i++) {
          if (board[i] == 1 && board[i + 4] == 0) {
            makeMove(movesArrayList,i, i + 4);
          }
          if (board[i] == 1 && board[i + 3] == 0) {
            makeMove(movesArrayList,i, i + 3);
          }
        }
        for (int i = 17; i <= 20; i++) {
          if (board[i] == 1 && board[i + 5] == 0) {
            makeMove(movesArrayList,i, i + 5);
          }
          if (board[i] == 1 && board[i + 4] == 0) {
            makeMove(movesArrayList,i, i + 4);
          }
        }
    
        for (int i = 21; i <= 24; i++) {
          if (board[i] == 1 && board[i + 4] == 0) {
            makeMove(movesArrayList,i, i + 4);
          }
          if (board[i] == 1 && board[i + 3] == 0) {
            makeMove(movesArrayList,i, i + 3);
          }
        }
    
    //System.out.println(movesArrayList);
    
        return movesArrayList.toArray();
    
      }
      public void makeMove(ArrayList list, int pos, int value) {
        Move aMove=new Move();
        aMove.setValues(pos, value);
        list.add(aMove);
      }
    
    
    }
    Last edited by sjalle; 03-18-2005 at 06:22 AM.
    eschew obfuscation

  11. #11
    Join Date
    Mar 2005
    Posts
    7

    The reason for this

    This is all for a artificial intelligence robot that I have to create for a checkers assignment. I was wondering, if you had some free time and didnt mind helping me through it, I would be most grateful.

    Id like to send you some code for the referee and humanPlayer classes and Im busy working on working out all legal moves to place into a minimax alogorith to produce an robot player.

    Im now trying to figure out how to implment this array into a minimax tree and return a result that can be used in the getMove() method from the driver class?????

    If you care to share your thoughts get me at warren_lennox@hotmail.com

    best regards

    warren

  12. #12
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Time is a precious thing

    This sounds like a rather time consuming job, and I am not sure that I have
    enough of that nowadays, but you could zip the stuff and mail it to:

    sjalle@start.no

    And I can have a look at it.

    Btw, the exeption that I got when I ran your program, and later, all my other
    snippets was in effect like: you are not allowed to instatiate custom classes


    When I reverted back to java 1.4, killed all the class files, - deleted the project,
    and restarted my computer, all was fine again..... Tiger.... hmmmfffh!!??
    Last edited by sjalle; 03-19-2005 at 01:15 PM.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center