Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java: simple array question

Posted on 2011-02-11
14
Medium Priority
?
257 Views
Last Modified: 2012-05-11
Hello experts;

I have this
Object[] params

I would like to add a parameter at the end
params[params.length]="extra"

But I got an error.
How to add a new item in the params ?
0
Comment
Question by:yarekGmail
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 34869852
try

params[params.length - 1]="extra"
0
 

Author Comment

by:yarekGmail
ID: 34869855
No !
I need to ADD a new value, not to replace the last element !
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34869856
and you have to set the size before setting value,

Object[] params = new Object[size] ;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:ksivananth
ID: 34869857
you have resize array if you want to add!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34869865
check this example to resize, http://www.source-code.biz/snippets/java/3.htm
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34869867
you better use some collectionlike ArrayList to avoid these hassles!
0
 

Author Comment

by:yarekGmail
ID: 34869898
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
}
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34869928
>>//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!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34869956

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";
}


0
 
LVL 47

Expert Comment

by:for_yan
ID: 34869962
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";
}


0
 
LVL 47

Expert Comment

by:for_yan
ID: 34869967


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;
}
0
 
LVL 92

Expert Comment

by:objects
ID: 34869968
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
0
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 34869985

and in the calling program, I believe you can
just call it like that:

param = appConnect(conn, param);



 
0
 
LVL 13

Assisted Solution

by:darren-w-
darren-w- earned 1000 total points
ID: 34870602
my 5p worth http://www.source-code.biz/snippets/java/3.htm this comes top of googleing 'java resizing an array' so :

you cant resize an standard array.

you have two options
1. create a new array that is a bigger size and copy the existing arrays contents into it
or
2. use a dynamic array from the new collection framework http://download.oracle.com/javase/1.4.2/docs/guide/collections/index.html, so dump your existing array into a suitable collection item such as an arraylist add what you want to the end then create a new array with the new size  and put the data back into it .
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

877 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