Danny King
Danny King

Reputation: 1991

Java array assignment (multiple values)

I have a Java array defined already e.g.

float[] values = new float[3];

I would like to do something like this further on in the code:

values = {0.1f, 0.2f, 0.3f};

But that gives me a compile error. Is there a nicer way to define multiple values at once, rather than doing this?:

values[0] = 0.1f;
values[1] = 0.2f;
values[2] = 0.3f;

Thanks!

Upvotes: 33

Views: 175960

Answers (10)

Hussein Muhammed
Hussein Muhammed

Reputation: 1

for example i tried all above for characters it fails but that worked for me >> reserved a pointer then assign values

char A[];
A = new char[]{'a', 'b', 'a', 'c', 'd', 'd', 'e', 'f', 'q', 'r'};

Upvotes: 0

Utkarsh Prajapati
Utkarsh Prajapati

Reputation: 1

int a[] = { 2, 6, 8, 5, 4, 3 }; 
int b[] = { 2, 3, 4, 7 };

if you take float number then you take float and it's your choice

this is very good way to show array elements.

Upvotes: -1

Genc Gashi
Genc Gashi

Reputation: 3

    public class arrayFloats {
      public static void main (String [] args){
        float [] Values = new float[3];
        float Incre = 0.1f;
        int Count = 0;

        for (Count = 0;Count<3 ;Count++ ) {
          Values [Count] = Incre + 0.0f;
          Incre += 0.1f;
          System.out.println("Values [" + Count + "] : " + Values [Count]);
        }


      }
    }

//OUTPUT:
//Values [0] : 0.1
//Values [1] : 0.2
//Values [2] : 0.3

This isn't the all and be all of assigning values to a specific array. Since I've seen the sample was 0.1 - 0.3 you could do it this way. This method is very useful if you're designing charts and graphs. You can have the x-value incremented by 0.1 until nth time.

Or you want to design some grid of some sort.

Upvotes: 0

skaffman
skaffman

Reputation: 403441

Yes:

float[] values = {0.1f, 0.2f, 0.3f};

This syntax is only permissible in an initializer. You cannot use it in an assignment, where the following is the best you can do:

values = new float[3];

or

values = new float[] {0.1f, 0.2f, 0.3f};

Trying to find a reference in the language spec for this, but it's as unreadable as ever. Anyone else find one?

Upvotes: 66

Julian Lettner
Julian Lettner

Reputation: 3659

On declaration you can do the following.

float[] values = {0.1f, 0.2f, 0.3f};

When the field is already defined, try this.

values = new float[] {0.1f, 0.2f, 0.3f};

Be aware that also the second version creates a new array. If values was the only reference to an already existing field, it becomes eligible for garbage collection.

Upvotes: 9

bou22
bou22

Reputation: 1

You may use a local variable, like:

    float[] values = new float[3];
    float[] v = {0.1f, 0.2f, 0.3f};
    float[] values = v;

Upvotes: 0

Bart van Heukelom
Bart van Heukelom

Reputation: 44074

This should work, but is slower and feels wrong: System.arraycopy(new float[]{...}, 0, values, 0, 3);

Upvotes: 0

Stephen C
Stephen C

Reputation: 718658

Java does not provide a construct that will assign of multiple values to an existing array's elements. The initializer syntaxes can ONLY be used when creation a new array object. This can be at the point of declaration, or later on. But either way, the initializer is initializing a new array object, not updating an existing one.

Upvotes: 4

fastcodejava
fastcodejava

Reputation: 41077

If you know the values at compile time you can do :

float[] values = {0.1f, 0.2f, 0.3f};

There is no way to do that if values are variables in runtime.

Upvotes: 1

bmargulies
bmargulies

Reputation: 99993

values = new float[] { 0.1f, 0.2f, 0.3f };

Upvotes: 2

Related Questions