# Thread: pretty simple array question

1. Member
Join Date
Dec 2009
Posts
15
Rep Power
0

## [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.

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. Senior Member
Join Date
Nov 2009
Posts
236
Rep Power
12
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. 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.

4. Member
Join Date
Dec 2009
Posts
15
Rep Power
0
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
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. Member
Join Date
Dec 2009
Posts
15
Rep Power
0
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?
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. 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. Member
Join Date
Dec 2009
Posts
15
Rep Power
0
Because I've been using java for 6 days and have no idea what a wrapper is. :D

9. Originally Posted by 711groove
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

#### Posting Permissions

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