Java equivalent of operators

Status
Not open for further replies.
Level 19
Joined
Aug 8, 2007
Messages
2,765
Just wondering, is there a java equivalent of

method operator []
method operator []=
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
Please explain what you mean (what do these opperators do in C++?).

Not in C++, in vJass o_O

for ex

JASS:
method operator []= takes integer int, integer int2 returns nothing
     call BJDebugMsg(I2S(int))
endmethod

(probably not the right syntax but oh well)

could be caled by

set WhateverStruct[134] = 456
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,241
Java does not support custom array assignment operators. All arrays in Java are hard coded and work the same (an array of custom type is just a typecast of Object array). Primitive types (like int) have their own array classes as they are not objects. The reason for this is they are optimized to use native array instructions for speed unlike JASS which is interpreted at all time (Java runs 1000s of times faster than JASS).

What you want is a method which takes an int representing index and the value to set and is called something like arrayasignment. This will convert the following in vJASS...
set WhateverStruct[134] = 456
to...
WateverObject.arrayasignment(134, 456);

This can be seen in classes that implement the List interface as that allows array like behaviour in a dynamic way. Infact, what you are probably after is making your class extend List and implementing the methods as all the methods are intended for custom array like behaviour (see ArrayList class for an example).

This is programmatically better as the List and other custom data structure interfaces in java.util are meant to allow interaction with a Object in such a way it mirrors a data structure type. For example List for an ordered container that very much mirrors an array (there are static utility methods to map an array into a List interface). Set for an object with set like properties (every element is unique). Collection for some generic unordered container (note that most like List extend Collection).
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
Java does not support custom array assignment operators. All arrays in Java are hard coded and work the same (an array of custom type is just a typecast of Object array). Primitive types (like int) have their own array classes as they are not objects. The reason for this is they are optimized to use native array instructions for speed unlike JASS which is interpreted at all time (Java runs 1000s of times faster than JASS).

What you want is a method which takes an int representing index and the value to set and is called something like arrayasignment. This will convert the following in vJASS...
set WhateverStruct[134] = 456
to...
WateverObject.arrayasignment(134, 456);

This can be seen in classes that implement the List interface as that allows array like behaviour in a dynamic way. Infact, what you are probably after is making your class extend List and implementing the methods as all the methods are intended for custom array like behaviour (see ArrayList class for an example).

This is programmatically better as the List and other custom data structure interfaces in java.util are meant to allow interaction with a Object in such a way it mirrors a data structure type. For example List for an ordered container that very much mirrors an array (there are static utility methods to map an array into a List interface). Set for an object with set like properties (every element is unique). Collection for some generic unordered container (note that most like List extend Collection).

I see. Thank you for the explanation! Mainly just wanted it for readability
 
Status
Not open for further replies.
Top