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.

1
  • 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<ArrayList<Double>> (both rows and columns are dynamic, but you don't need to worry about the rows anyway) Commented May 8, 2013 at 9:28

6 Answers 6

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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]>.

Comments

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

Comments

0

If you know the element size at runtime

int size=runtime decides;
double [][] lastSecArray = new double [2][size];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.