Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a double array without a fixed size?

Hello i have a problem with c# Arrays. I need a array to store some data in there... My Code is that

double[] ATmittelMin;
ATmittelMin[zaehlMittel] = Gradient(x, xATmax, y, yATmax);

But the compiler says: not defined var How can i define a double array without a fixed size ? Thanks a lot!

like image 423
subprime Avatar asked Jun 22 '09 08:06

subprime


People also ask

How do you define a double array?

To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.

How do you declare a two-dimensional array in Java without size?

Declaring 2-D array in Java: Any 2-dimensional array can be declared as follows: Syntax: data_type array_name[][]; (OR) data_type[][] array_name; data_type: Since Java is a statically-typed language (i.e. it expects its variables to be declared before they can be assigned values).

How do you declare a double array in Java?

To create a two dimensional array in Java, you have to specify the data type of items to be stored in the array, followed by two square brackets and the name of the array. Here's what the syntax looks like: data_type[][] array_name; Let's look at a code example.

What do you call a 2-dimensional array?

The 2D array is organized as matrices which can be represented as the collection of rows and columns.


1 Answers

Arrays are always fixed in size, and have to be defined like so:

double[] items1 = new double[10];

// This means array is double[3] and cannot be changed without redefining it.
double[] items2 = {1.23, 4.56, 7.89};

The List<T> class uses an array in the background and redefines it when it runs out of space:

List<double> items = new List<double>();
items.Add(1.23);
items.Add(4.56);
items.Add(7.89);

// This will give you a double[3] array with the items of the list.
double[] itemsArray = items.ToArray();

You can iterate through a List<T> just as you would an array:

foreach (double item in items)
{
    Console.WriteLine(item);
}

// Note that the property is 'Count' rather than 'Length'
for (int i = 0; i < items.Count; i++)
{
    Console.WriteLine(items[i]);
}
like image 71
Blixt Avatar answered Sep 23 '22 00:09

Blixt