Results 1 to 8 of 8
  1. #1
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default Conversion of an arraylist to a 2D array of integers

    Conversion of an arraylist to a two dimensional array which contains integers

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by yanipao View Post
    Conversion of an arraylist to a two dimensional array which contains integers
    Wishing you much luck with that.

    Note that if you have an actual question, I strongly suggest that you do that, ask a question (I don't see one above), and also provide enough information so that it's answerable (right now as far as details, you don't give much). So again, about all I can do is wish you luck. :)
    Last edited by Fubarable; 02-03-2010 at 04:45 AM.

  3. #3
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by yanipao View Post
    Conversion of an arraylist to a two dimensional array which contains integers
    its pretty broad and a vague question..

    try giving some examples and narrowing the question. Displaying the code is even better.

  4. #4
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    Java Code:
    int size = arrlist.size();
    secondSize = size /2;
    int secondSize = size%2 == 0 ? secondSize : secondSize+1;
    int[][] intArray = new int[2][secondSize];
    for(int i=0;i<size;i++){
    if(i > secondSize){
    intArray[1][i%secondSize] = arrlist.get(i);
    } else{
    intArray[0][i]=arrlist.get(i);
    }
    }
    Enjoy, ofcouse the simplest case is to say new int[1][size] but thats a bit cheecky no?

  5. #5
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    On a second look that i > secondSize may need to be >=

  6. #6
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    may need a intValue() call as well

  7. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by JavaJuJitZu View Post
    Java Code:
    int size = arrlist.size();
    secondSize = size /2;
    int secondSize = size%2 == 0 ? secondSize : secondSize+1;
    int[][] intArray = new int[2][secondSize];
    for(int i=0;i<size;i++){
    if(i > secondSize){
    intArray[1][i%secondSize] = arrlist.get(i);
    } else{
    intArray[0][i]=arrlist.get(i);
    }
    }
    Will this code compile?. i mean excluding the class tags.

  8. #8
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    Java Code:
     /* declare a list and fill it */
            ArrayList<Integer> arrlist = new ArrayList<Integer>();
            arrlist.add(3);
            arrlist.add(4);
            arrlist.add(5);
            arrlist.add(10);
            arrlist.add(2);
            arrlist.add(23);
            arrlist.add(1);
    
            /* Original Code with a few fixes */
            int size = arrlist.size();
            int secondSize = size / 2;
            secondSize = size % 2 == 0 ? secondSize : secondSize + 1;
            int[][] intArray = new int[2][secondSize];
            for (int i = 0; i < size; i++) {
                if (i >= secondSize) {
                    intArray[1][i % secondSize] = arrlist.get(i);
                } else {
                    intArray[0][i] = arrlist.get(i);
                }
            }
            /* foreach loop on the multidimensional array */
            for(int[] i : intArray){
                for(int j : i){
                    System.out.println("A Number in the array " + j);
                }
            }
    Just stick that in a main and see for yourself, had to fix the 2nd and third line of the original, the int is on the wrong line.
    Last edited by JavaJuJitZu; 02-04-2010 at 11:09 AM. Reason: typo

Similar Threads

  1. file input: array of integers
    By hannes in forum New To Java
    Replies: 8
    Last Post: 01-27-2010, 03:44 PM
  2. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 08:58 AM
  3. Replies: 7
    Last Post: 04-28-2008, 04:28 AM
  4. Replies: 2
    Last Post: 04-16-2008, 12:35 PM
  5. Arraylist to a 2- dimension array conversion
    By mars123 in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •