?
Solved

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

Posted on 1999-11-09
9
Medium Priority
?
4,666 Views
Last Modified: 2010-07-27
I need Java syntax equal for memset C++

int test[20];
memset ( test, 0, text.length()-1);
0
Comment
Question by:JavaBoy060299
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 2

Expert Comment

by:_lychee_
ID: 2193572
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
 

Author Comment

by:JavaBoy060299
ID: 2193702
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
 
LVL 2

Expert Comment

by:_lychee_
ID: 2193724
oops... in that case i dunno...
if u have another 0 array u could use System.arrayCopy though :>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 2

Expert Comment

by:_lychee_
ID: 2193727
sorry... that would be arraycopy not arrayCopy
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2194254
i've always thought that integers already have default value = 0 ...
0
 
LVL 5

Expert Comment

by:Jod
ID: 2195878
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
 

Author Comment

by:JavaBoy060299
ID: 2199699
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
 
LVL 5

Accepted Solution

by:
Jod earned 30 total points
ID: 2200002
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
 

Author Comment

by:JavaBoy060299
ID: 2207716
Jod, I also know about that, I just want do it in different way
btw Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month6 days, 10 hours left to enroll

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question