yarekGmail
asked on
java: simple array question
Hello experts;
I have this
Object[] params
I would like to add a parameter at the end
params[params.length]="ext ra"
But I got an error.
How to add a new item in the params ?
I have this
Object[] params
I would like to add a parameter at the end
params[params.length]="ext
But I got an error.
How to add a new item in the params ?
ASKER
No !
I need to ADD a new value, not to replace the last element !
I need to ADD a new value, not to replace the last element !
and you have to set the size before setting value,
Object[] params = new Object[size] ;
Object[] params = new Object[size] ;
you have resize array if you want to add!
check this example to resize, http://www.source-code.biz/snippets/java/3.htm
you better use some collectionlike ArrayList to avoid these hassles!
ASKER
5 answers and no one fits my needs...
this is my function
public boolean appConnect( IConnection conn , Object[] params ){
// I need to add an extra value to params
params[params.length] = "new value"; //gives me an error since the params needs to be resized
}
this is my function
public boolean appConnect( IConnection conn , Object[] params ){
// I need to add an extra value to params
params[params.length] = "new value"; //gives me an error since the params needs to be resized
}
>>//gives me an error since the params needs to be resized
see my comment #34869865
>>5 answers and no one fits my needs...
how? did u try my suggestions?
>>this is my function
the resize would allow u to add a new value but you can't expect that reflected in the caller. for that you need to deal with ArrayList as I told earlier!
see my comment #34869865
>>5 answers and no one fits my needs...
how? did u try my suggestions?
>>this is my function
the resize would allow u to add a new value but you can't expect that reflected in the caller. for that you need to deal with ArrayList as I told earlier!
You have to use a new array, see below
better to use ArrayLists
this is my function
public boolean appConnect( IConnection conn , Object[] params ){
// I need to add an extra value to params
params[params.length] = "new value"; //gives me an error since the params needs to be resized
Object [] params1 = new Object[params.length+1];
for(int j =0; j<params.length; j++){
prams1[j]=params[j];
}
params1[params.length] = "extra";
}
of couse this operator which gives error should be removed:
public boolean appConnect( IConnection conn , Object[] params ){
// I need to add an extra value to params
//params[params.length] = "new value"; //gives me an error since the params needs to be resized
Object [] params1 = new Object[params.length+1];
for(int j =0; j<params.length; j++){
prams1[j]=params[j];
}
params1[params.length] = "extra";
}
public boolean appConnect( IConnection conn , Object[] params ){
// I need to add an extra value to params
//params[params.length] = "new value"; //gives me an error since the params needs to be resized
Object [] params1 = new Object[params.length+1];
for(int j =0; j<params.length; j++){
prams1[j]=params[j];
}
params1[params.length] = "extra";
}
And you return this new array:
public Object[] appConnect( IConnection conn , Object[] params ){
// I need to add an extra value to params
//params[params.length] = "new value"; //gives me an error since the params needs to be resized
Object [] params1 = new Object[params.length+1];
for(int j =0; j<params.length; j++){
prams1[j]=params[j];
}
params1[params.length] = "extra";
return params1;
}
following explains resizing an array which you'll need to do before adding the element
http://helpdesk.objects.com.au/java/how-do-i-resize-a-java-array
http://helpdesk.objects.com.au/java/how-do-i-resize-a-java-array
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
params[params.length - 1]="extra"