Home arrow static arrow Java Programming [Archive] - How to initialize the array with some default value?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to initialize the array with some default value?
This topic has 22 replies on 2 pages.    1 | 2 | Next »

Posts:253
Registered: 11/5/03
How to initialize the array with some default value?  
Jul 16, 2004 5:33 AM



 
Hi All,
How can i initialize an Array with default value during defining.

int p[] = new int[30];

And I want p[0] to p[29] to be initialized to 34. How can i do it?

I will thankful,

Regards,
Ravi.
 

Posts:319
Registered: 11/8/00
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:39 AM (reply 1 of 22)



 
int p[] = new int[30]{34,34,34,34,34,34,34,34,34,34,34........};

I'm sure there will be a quicker way but i can't think of it off the top of my head.
 

Posts:319
Registered: 11/8/00
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:40 AM (reply 2 of 22)



 
int p[] = new int[30]{34,34,34,34,34,34,34,34,34,34,34........};

you don't need the 30, i'm not sure if it works with it there.
 

Posts:253
Registered: 11/5/03
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:43 AM (reply 3 of 22)



 
I don't think I will insert value like that manually repeating the same stuff and especially whe array size is allocated dynamically. How can initialize it? Can anyone else think of a method which will do that for me in java ?
 

Posts:1,995
Registered: 12/10/00
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:51 AM (reply 4 of 22)



 
Hi Ravi, nice to see you again ;)

int p[] = new int[30];Arrays.fill(p, 34);


But this is not exactly what you wanted
Mike
 

Posts:319
Registered: 11/8/00
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:52 AM (reply 5 of 22)



 
This does it a bit quicker,

int[] p = new int[30];for (int i = 0; i < p.length; i++){    p[i] = 34;}
 

Posts:6,487
Registered: 5/5/04
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:52 AM (reply 6 of 22)



 
public int[] allocateIntArray(int size, int value) {    int[] ret = new int[size];    for(int j=0;j<size;j++)        ret[j] = value;    return ret;}
 

Posts:319
Registered: 11/8/00
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:53 AM (reply 7 of 22)



 
you beat me to it bellyripper
 

Posts:1,044
Registered: 4/16/04
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:55 AM (reply 8 of 22)



 
int totalArraySize = getArraySize(); // a method to return the dynamic size of the arrayint[] num = new int[totalArraySize]; // getArraySize() could even be passed in directly instead of the intfor(int x = 0; x < num.length; x++) {    num[x] = 34;} 
ICE
 

Posts:1,044
Registered: 4/16/04
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:56 AM (reply 9 of 22)



 
Boy is this forum interesting!

ICE
 

Posts:7,499
Registered: 02-11-14
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:57 AM (reply 10 of 22)



 
I don't think I will insert value like that manually
repeating the same stuff and especially whe array
size is allocated dynamically. How can initialize it?
Can anyone else think of a method which will do that
for me in java ?

How can i initialize an Array with default value during defining
The method stated above is the only way, as far as I know, to do it while it is being defined...

But you can do it this:
int SIZE = 30; int DEFAULT = 34;int[] p = new int[SIZE];for (int c = 0; c < SIZE; c++) p[c] = DEFAULT;


If you want to initialize a class member like that you could use a static initialiser:
class SomeClass {  private static final int SIZE=30, DEFAULT=34;  int[] p = new int[SIZE];  { for (int c=0; c<SIZE; c++) p[c]=DEFAULT; }  public SomeClass() { /*...*/}}
See how the for loop is in the brackets? That will run the code before the constructor is called, I believe.
 

Posts:7,499
Registered: 02-11-14
Re: How to initialize the array with some default value?  
Jul 16, 2004 5:58 AM (reply 11 of 22)



 
Wow, I was a mile slower than everyone else :(
 

Posts:1,044
Registered: 4/16/04
Re: How to initialize the array with some default value?  
Jul 16, 2004 6:05 AM (reply 12 of 22)



 
It is soooooooooooooo interesting to see how different people provide different code styles just to solve the same problem.

You turn around for second in this forum and before you know it, a gazillion posts before yours. I think I need to go for typing lessons. I'm too **** slow. (well at least I was slightly faster)

ICE
 

Posts:253
Registered: 11/5/03
Re: How to initialize the array with some default value?  
Jul 16, 2004 6:36 AM (reply 13 of 22)



 
Hey it fun that u guys are there still :). Thanks for all your posts I think I will the best method out of it. But is there any way to set memory content to default value during dynamic allocation time itself. Because I assume that will be much faster.
 

Posts:319
Registered: 11/8/00
Re: How to initialize the array with some default value?  
Jul 16, 2004 6:43 AM (reply 14 of 22)



 
yes, as i said before:

int p[] = new int[]{34,34,34,34,34,34,34,34,34,34,34........};
 
This topic has 22 replies on 2 pages.    1 | 2 | Next »