4

I am new to java and I want to create a 2-dimensional array in which the rows are static but the columns are dynamic.

double [][] lastSecArray = new double [2][41];
int lastValue = -1;
public foo (){
   lastValue++;
   lastSecArray[0][lastValue] = st_ms;
   //more code here
}

The value of lastValue increases and when it reaches 41 my program gives me Array Index out of bound. Which is what I should expect.

How can I make the column of teh array dynamic so that no matter how large the value of lastValue increases it runs.

bytecode77
  • 14,163
  • 30
  • 110
  • 141
Wearybands
  • 2,438
  • 8
  • 34
  • 53
  • An array of `ArrayList`... but since Java doesn't allow array of generic (you can create non-generic `ArrayList[]`, but there will be warning about type safety), so `ArrayList>` (both rows and columns are dynamic, but you don't need to worry about the rows anyway) – nhahtdh May 08 '13 at 09:28

6 Answers6

3

It may be more appropriate to use a Map<Double, List<Double>>. Having the value of the Map as a List<Double> will allow you to expand the list as opposed to an array which has a fixed size.

public static void main(String[] args) throws CloneNotSupportedException {
    Map<Double, List<Double>> myMap = create(1, 3);
}

public static Map<Double, List<Double>> create(double row, double column) {
    Map<Double, List<Double>> doubleMap = new HashMap<Double, List<Double>>();

    for (double x = 0; x < row; x++) {
        for (double y = 0; y < column; y++) {
            doubleMap.put(x, new ArrayList<Double>());
        }
    }
    return doubleMap;
}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
3

Try to use

Map<String, ArrayList> lastSecArray = new HashMap<>();
    ArrayList value = new ArrayList();
    value.add(0);
    value.add(1);
    value.add(2);
    lastSecArray.put("0", value);

so you can operate with

lastSecArray.size()

or

lastSecArray.put(...)

or array in array

1

Use ArrayList instead.

You can use:

List<List<Double>> = new ArrayList<>();

ArrayList is dynamically-extendable. You can create ArrayList both for rows and cols.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1

I assume you're coming from the C-world. In Java, you have many objects which represent arrays.

I suggest you to check this link : ArrayList. This is a class which uses a resizable array.

I think this is a good way to have a dynamic two dimensionnal Array in Java.

As ArrayList is a template class, you are able to create ArrayList<ArrayList<double>>, or if you want to have a "static" number of rows, you can create ArrayList<double[2]>.

abletterer
  • 109
  • 4
0

in Java arrays are fixed size so what you are saying is not possoble.

take a look at this thread might help you.

Variable length (Dynamic) Arrays in Java

Community
  • 1
  • 1
pulasthi
  • 1,730
  • 1
  • 17
  • 29
0

If you know the element size at runtime

int size=runtime decides;
double [][] lastSecArray = new double [2][size];
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307