# Generic Array Tools (lend me a critical eye)

• February 28th, 2014, 12:16 PM
KAJLogic
Generic Array Tools (lend me a critical eye)
Is there a problem with this conceptually - as in do you foresee any issues?

Code :

```/* * Trying to make a universal tool for increment an array by one while keeping all the previous values in place. * */ public K[] increment(K[] k){ int i = 0; K[] tmp = (K[])new Object[Array.getLength(k)+1]; /* * Parses through the passed k and fills tmp with all of ks values * */ for(K kk: k){ tmp[i] = kk; ++i; } return tmp; }```

Consequently then this.

Code :

```/* * Same with the first one, but for 2D arrays. * */ public K[][] increment(K[][] k){ int i = 0; int ii = 0; K[][] tmp = (K[][])new Object[Array.getLength(k)+1][]; for(K[] kara: k){   // instantiate a new array at i with the length of the current iteration ( kara )   tmp[i] = (K[])new Object[Array.getLength(kara)]; for(K kay: kara){   // fill tmp's array at i with the same values of kara   tmp[i][ii]=kay; ++ii; } ++i; ii=0; } return tmp; }```
• February 28th, 2014, 12:19 PM
Norm
Re: Generic Array Tools (lend me a critical eye)
What is the code supposed to do? Add some comments describing what it does and any problems it had to solve to get it to work.
• February 28th, 2014, 12:24 PM
KAJLogic
Re: Generic Array Tools (lend me a critical eye)
Apologies I did that. while I was looking at it I noticed I probably could have for the 2D array simply made tmp[i] = kara.

Code :

```        /* * Same with the first one, but for 2D arrays. * */ public K[][] increment(K[][] k){ int i = 0; K[][] tmp = (K[][])new Object[Array.getLength(k)+1][]; for(K[] kara: k){   // just make tmp[i] = kara iteration // essentially use Java's built in array copy tool tmp[i] = kara; ++i;   }```

The only thing that makes me a little uneasy about this is my lack of knowledge on how an array is copied by the API implementation.
• February 28th, 2014, 12:41 PM
Norm
Re: Generic Array Tools (lend me a critical eye)
The Arrays class has methods that do some of that more efficiently.
• February 28th, 2014, 12:45 PM
KAJLogic
Re: Generic Array Tools (lend me a critical eye)
which part?
• February 28th, 2014, 01:02 PM
Norm
Re: Generic Array Tools (lend me a critical eye)
The copy part
• February 28th, 2014, 01:12 PM
copeg
Re: Generic Array Tools (lend me a critical eye)
I'm not sure about the goal here, but if you are wishing for a more dynamic array perhaps use a List. Alternatively you can make your methods more flexible by adding a parameter which allows one to expand the array by a given value (rather than 1 every time).

Also see System (Java Platform SE 7 )

And lastly, are you looking to make a copy of the array, or reference the same (your 2D array references...and it should be noted in the comments that it does this - thus changes in the parameter array affect the returned array)
• February 28th, 2014, 02:28 PM
KAJLogic
Re: Generic Array Tools (lend me a critical eye)
Can you elaborate on your lastly; where does it reference?

• February 28th, 2014, 02:37 PM
copeg
Re: Generic Array Tools (lend me a critical eye)
Quote:

Originally Posted by KAJLogic
Can you elaborate on your lastly; where does it reference?

```public static void main(String[] args){ int[][] a = {{1,2},{3,4}}; int[][] b = new int[a.length+1][]; for ( int i = 0; i < a.length; i++ ){ b[i] = a[i]; }   System.out.println(Arrays.toString(a[0])); System.out.println(Arrays.toString(b[0])); a[0][0] = 10000; System.out.println(Arrays.toString(a[0])); System.out.println(Arrays.toString(b[0])); } /* prints: [1, 2] [1, 2] [10000, 2] [10000, 2]   */```
```/* * Same with the first one, but for 2D arrays. * */ public K[][] increment(K[][] k){ int i = 0; int ii = 0; K[][] tmp = (K[][])new Object[Array.getLength(k)+1][]; for(K[] kara: k){   // instantiate a new array at i with the length of the current iteration ( kara )   tmp[i] = (K[])new Object[Array.getLength(kara)]; for(K kay: kara){   // fill tmp's array at i with the same values of kara   tmp[i][ii]=kay; ++ii; } ++i; ii=0; } return tmp; }```