Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing array to containg single value of elements  RSS feed

 
Michael Boehm
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I am confusing this with something from another language, but I seem to remember that it is possible to initialize an array of some length such that all elements are set to an initial non-default value. I have searched around but havent found anything. Perhaps I am misremembering ?
 
vicky Aggarwal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one possible solution is following

int a [] = {1,2,3,4,5};

if it helps.
 
Michael Boehm
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to initialise an array with an already defined length to have all elements equal to a given element. I am now almost sure I have done this before, but don't rememeber how.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

do you mean like

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Boehm wrote:I want to initialise an array with an already defined length to have all elements equal to a given element. I am now almost sure I have done this before, but don't rememeber how.


If the array "already [has a] defined length", then the array object has already been instantiated -- and the OP is looking to set the same object to all the elements. The simplest option that I can think of is to use a loop.

Henry
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps I am confusing this with something from another language

Possibly some variant of xBase. I know it can be done in VFP 6.

No, you can't do this in Java without looping over the array and setting the value of each element.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Boehm wrote:I want to initialise an array with an already defined length to have all elements equal to a given element.
Have a look at the Arrays class
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:No, you can't do this in Java without looping over the array and setting the value of each element.

There's a method in class java.util.Arrays to fill all elements of an array with the same value. Look it up in the API documentation.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a method in class java.util.Arrays ...

Thank you Jesper, I wasn't aware of that.
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good stuff:
 
Michael Boehm
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I must be getting old. I had almost convinced myself that there was some way of doing this and even believing I rememebered seeing somewhere a discussion that it was supported according to the spec. Oh, well Thanks for the pointer to the lesser known method in Arrays.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!