Java Syntax for give default value for array, like memset in C++

I need Java syntax equal for memset C++

int test[20];
memset ( test, 0, text.length()-1);
JavaBoy060299Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_lychee_Commented:
i guess
memset(test,0,text.length()-1)
sets test[0], [1]...[text.length()-1] to 0?

u can use
Arrays.fill for that kinda functionality... (fill is a static method in the class java.util.Arrays
0
JavaBoy060299Author Commented:
lychee, yup it is true
but I don't want to fill it with 0 manually.
unfortunatelly I'm using JDK 1.1.7a
so there is no java.util.Arrays class
0
_lychee_Commented:
oops... in that case i dunno...
if u have another 0 array u could use System.arrayCopy though :>
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

_lychee_Commented:
sorry... that would be arraycopy not arrayCopy
0
heyhey_Commented:
i've always thought that integers already have default value = 0 ...
0
JodCommented:
Yep, they are.

Default initialisation for member variables is:

boolean false
char    NULL
byte    0
short   0
int     0
long    0
float   0.0
double  0.0

For variables in methods you will be forced to initialise the variables by the compiler anyway.

If you want to replicate memset then do something like this:

public void memset(int[] ia, int initValue)
  for (int i = 0; i < ia.length; i++) {
    ia[i] = initValue;
  }
}

This will work for integers - you will need other version for other primitive types.



0
JavaBoy060299Author Commented:
Jod, sorry have to reject your answered
please see my previous comments

>but I don't want to fill it with 0 >manually

I know how to do it manually, but I need something special like memset in C++.

heyhey_, yes default value will be 0 for integers, but How to make it 0 again, because I have already give some value before.
0
JodCommented:
Sorry didn't know what you meant by manually. Thought you were trying to do:

ia[0] = 0;
ia[1] = 1;

etc...by hand.

One quick way to cheat is to just recreate your array and initialise it with a new array of int:

eg:

//create array
int[] arrayofint

[loop using arrayofint]
   // this will re-initialise your
   // array of int on each iteration
   // of the loop
   arrayofint = new int[30];
   ...
   //use array etc...
   ...
[end loop]



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JavaBoy060299Author Commented:
Jod, I also know about that, I just want do it in different way
btw Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.