Results 1 to 9 of 9
  1. #1
    711groove is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Thumbs up [SOLVED] pretty simple array question

    This is my code (which doesn't work)

    Java Code:
        for (int makeArray = 1 ; makeArray < numberOfArrays ; makeArray++){
        	int ???????? = {0,1,2,3,4,5,6,7,8,9};
        }
    What do I put for ?????????? to make an array whos name is the current value of makeArray? Just putting makeArray doesn't work, because that just tries to redfine the integer makeArray, instead of create a new array with its value.

    The point of this is to create (numberOfArrays) arrays with distinct, sequential names in which I can insert data at a later time as needed.

    Thank in advance.

    EDIT: or, if there's a way to split an array with X number of entries (for example, 521 entries) into Y number of arrays (for example, 53 arrays) with 10 entries each, that'd work as well.

    example:
    Java Code:
    massiveArray = {1,2,3,4,5,6,7,8,9 ... 321,322,323};
    smallerArrays = { {1,2,3,4},
    {5,6,7,8},
    ...
    {320,321,322,323} };
    EDIT2: also would work is a way to turn a single row in a jtable into multiple rows for the same max length

    basically I want ANY way to turn

    1,2,3,4,5,6,7,8,9
    into
    1,2,3
    4,5,6
    7,8,9

    if it's unknown how many entries there are
    Last edited by 711groove; 12-06-2009 at 09:59 AM.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    you can use a 2 dimensional array and use two for loops to set the values.
    int[] massiveArray = new int[] {1,2,3,4,5,6,7,8,9 ... 328,329,330};
    int[][] smallerArray = new int[30][10];
    for (int i=0; i<30; i++)
    {
    for (int j=0; j<10)
    {
    smallerArray[i][j] = massiveArray[i*10+j];
    }
    }

    Something like that will work I think.

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    I don't really understand what you are asking for, but for your last question, I would create a two-dimensional array.

    In this case, you would specify how many columns you wanted it (e.g. 3). The number of rows is Math.ceil((double) numOfElements / colSize).

    For example,

    Java Code:
    int[] elements = {1,2,3,4,5,6,7,8,9};
    int colSize = 3;
    int rowSize = (int) Math.ceil((double) elements.length / colSize);
    int[][] matrix = new int[rowSize][colSize];
    		
    for (int i = 0; i < elements.length; i++)
    {
    	matrix[i / colSize][i % colSize] = elements[i];
    }
    				
    System.out.println(java.util.Arrays.deepToString(matrix));
    This would create a 3x3 matrix initialized with the given elements.

    Edit:
    If you didn't have enough elements to fill the last row (e.g. 10 elements, 3 columns), then the remaining entries in the last row would be left uninitialized. In the case of an int, that would be a 0, whereas if you did this with objects, there would be nulls.
    Last edited by CodesAway; 12-06-2009 at 08:05 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #4
    711groove is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    Java Code:
        String[][] finalOut = new String[output.size()/10][10];
          for (int i = 0; i < output.size()/10 ; i++){
          	for (int j = 0 ; j < 10 ; j++){ 
          		thiss = Integer.toString(output.get(i*10+j));
          		finalOut[i][j] = new String[thiss];}}
    massivearray changed to 'output'
    smallerarray changed to 'finalOut'

    Needs to be string arrays for import to jtable

    I get 'incompatible types' for the 2nd to last line.

    I have no idea how to fix this, I've tried everything I can think of.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    String[][] finalOut = new String[output.size() / 10][10];
    for(int i = 0; i < output.size() / 10; i++) {
        for(int j = 0; j < 10; j++) { 
            finalOut[i][j] = Integer.toString(output.get(i * 10 + j));
        }
    }

  6. #6
    711groove is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    That's what I had at first.. didn't work so I tried defining the string before using it in a second line. same error either way. I combine the first two replies with a little googling and discovered my incompatible types problem was cause by massiveArray being an ArrayList (returning objects) not an array.

    Java Code:
        Object[][] rows = new Integer[output.size()/10 + 1][10];
        Object outputArray[] = output.toArray();
          for (int i = 0; i < output.size()/10 ; i++){
          	for (int j = 0 ; j < 10 ; j++){next = i * 10 + j;
          	rows[i][j] = outputArray[next];}}
    Works perfectly, except I lose incomplete lines at the end. working on a check that adds an extra line if needed, but that might fail, so suggestions are welcome in the meantime!

    EDIT: can't figure out a solution. partial lines just dont show up. Any ideas anyone?
    EDIT2: Doh! I simply added:
    Java Code:
    while (output.size() % 10 != 0){output.add(null);}
    it didn't work as a for loop, but that tiny little line simply makes sure output has an exact multiple of 10 number of entries, so that every line of outputArray gets filled!
    Last edited by 711groove; 12-06-2009 at 09:57 AM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Why do you people want to haul all those elements from array to array? Use a simple wrapper class; Java is an OO language after all:

    Java Code:
    public class ArrayWrapper {
    
       private int[] array;
       int rows, cols
    
       public ArrayWrapper(int[] array, int rows, int cols) {
          this.array= array;
          this.rows= rows;
          this.cols= cols;
       }
    
       private int index(int r, int c) {
          int i= r*cols+c;
          if (i < 0 || i >= array.length)
             throw new ArrayIndexOutOfBoundsException(i);
       }
    
       public int get(int r, int c) {
          return array[index(r, c)];
       }
    
       public void set(int r, int c, int value) {
          array[index(r, c)]= value;
       }
    }
    kind regards,

    Jos

  8. #8
    711groove is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default

    Because I've been using java for 6 days and have no idea what a wrapper is. :D

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by 711groove View Post
    Because I've been using java for 6 days and have no idea what a wrapper is. :D
    The little class above takes care of a one dimensional array and supplies a two dimensional view (through the set and get methods); it 'wraps' the array. Study it and see what its benefits are.

    kind regards,

    Jos

Similar Threads

  1. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 08:33 PM
  2. A simple List question
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 02-16-2009, 04:37 AM
  3. Simple question about access
    By tortelini in forum New To Java
    Replies: 6
    Last Post: 09-06-2008, 06:41 PM
  4. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 02:27 AM
  5. Simple question of JTable
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 08:07 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
  •