Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing an array to a java function

Status
Not open for further replies.

JennyP

Programmer
Jul 25, 2000
9
AT
I have:
var myArray = new Array();
myArray[0]="firststring";
myArray[1]="secondstring";
myArray[2]="thirdstring";

p.setMyArray(myArray); <-- this line does not work...

Where in p is a java class containing the following:
public void setMyArray(String[] v) { myArray= v; }

can anyone help me?
If it makes any difference, in the end, I want to write the string values to a multi-value attribute in LDAP.

Thanks
Jenny
 
i think that what is not working, is that you want to copy an array to another array using =
(ie the line : myArray= v; )
passing an array as a parameter is not a problem (i've done this and it was working fine) - creating an array from another requires first that you create the object (use : new)
for more info go to :
they have great & clear examples on this
let me know if it's enough ir if you need more explainations
 
still confused...

I also have this declaration in the Java class:
String[] myArray = new String[3];
 
no - if you read carefully what they say at devedge's, the proper way to copy an array in another is something like :
String[] newArray = new Array(oldArray.slice(0));

and they don't have any constructor like this : new Type[int] !

but, why are you filling &quot;myArray&quot; to immediatly replace it with a new array ? why do you need 2 arrays containing the same values after this ??
 
wondering ... aren't you confusing java and javascript ??? those are 2 totally different languages !!!
 
hmm... I guess it looks a bit confusing.
The first bit where I fill the array with values is in Javascript in an html page. I then create a java object and pass the filled array into the Java function setMyArray which is a fairly standard get/set property function.

In the second bit myArray = v; myArray is the internal property and v is the passing parameter and so should contain the values that were passed in.

does that make sense??

 
yes
you're in a jsp (or a servlet), right ? that's where you create p, and ask it to setMyArray(myArray), where myArray is a javascript object, right ?
sounds confusing, yes !!
i've never tried to do this, because i create &quot;myArray&quot; in pure java, so it's far easier to pass a java object to a java function ;]]
anyway, if you HAVE TO create myArray in javascript, you can try to (in java !) check the value of &quot;myArray&quot; (System.out.println(myArray) or something ...) before calling the function - is it what you expect ?
also, even in java, you can't copy arrays like this &quot;new_array = old_array&quot; ... you have to use the arraycopy function instead !!!
let me know how it goes - and if you're still stuck, you can also post your question in the java forum ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top