Dynamic Array tools
Platform: Java
Published May 01, 2012
Updated May 02, 2012
Working on a project recently, I came across the need for an array that could dynamically adjust its size for a fluctuating capacity. This is a neat little snippet that can very easily change the dimensions of an array. All without losing any of the stored data, unless of course you opt to shrink it. It works with both single and 2 dimensional arrays, I would've added support for 3 dimensional arrays, but I am yet to find a use for such an expansive array so, for now anyway, I've left it out. I have overloaded each of the methods so that it will accept both normal and 2D arrays.
To use:
-- shrink(String[] s, int amt) or shrink(String[][] s, int rows, int col)
-- expand(String[] s, int amt) or expand(String[][] s, int rows, int col)
I've embedded some examples in the code to demonstrate usage. Two-Dimensional arrays can be restructured by both rows and/or columns. Which means you could, by choice, change a 3 by 3 array into an 4 by 5 or so on. Negative numbers can also be specified. Enjoy. :)
public class ArrayAdapt {
public static void main(String[] args) { new ArrayAdapt(); }
public ArrayAdapt() {
String[][] a = { {"This","is","an","array"},
{"A","two","dimensional","array"} };
String[] b = { "This","is","an","array" };
//Shrink subtracts 1 index
System.out.println(b.length + " Pre-Shrink length");
b = shrink(b,1);
System.out.println(b.length + " Post-Shrink length\r\n");
//Two-Dimensional Array Shrink subtracts 1 row and 1 column
System.out.println(a.length + " " + a[0].length + " Pre-Shrink Array Rows/Columns values (2D)");
a = shrink(a,1,1);
System.out.println(a.length + " " + a[0].length + " Post-Shrink Array Rows/Columns values (2D)\r\n");
//Expand adds 1 index
System.out.println(b.length + " Pre-Expansion length");
b = expand(b,1);
System.out.println(b.length + " Post-Expansion length\r\n");
//Two-Dimensional Array Expand adds 1 row and 1 column
System.out.println(a.length + " " + a[0].length + " Pre-Expansion Rows/Columns lengths (2D)");
a = expand(a,1,1);
System.out.println(a.length + " " + a[0].length + " Post-Expansion Rows/Columns lengths (2D)\r\n");
}
public String[] shrink(String[] s, int amt) {
if (amt >= s.length) { return s; }
else {
String[] out = new String[(s.length - amt)];
for (int i = 0;i<out.length;i++) {
out[i]=s[i];
}
return out;
}
}
public String[][] shrink(String[][] s, int rows, int col) {
if (rows >= s.length || col >= s[0].length) { return s; }
else {
String[][] out = new String[(s.length - rows)][(s[0].length - col)];
for (int i = 0;i < out.length;i++) {
for (int a = 0;a < out[i].length;a++) {
out[i][a]=s[i][a];
}
}
return out;
}
}
public String[] expand(String[] s, int amt) {
String[] out = new String[(s.length + amt)];
for (int i = 0;i<out.length;i++) {
try { out[i]=s[i]; } catch (Exception e) { out[i] = ""; }
}
return out;
}
public String[][] expand(String[][] s, int rows, int col) {
String[][] out = new String[(s.length + rows)][(s[0].length + col)];
for (int i = 0;i < out.length;i++) {
for (int a = 0;a < out[i].length;a++) {
try { out[i][a]=s[i][a]; } catch (Exception e) { out[i][a]=""; }
}
}
return out;
}
}