Solved

java: simple array question

Posted on 2011-02-11
14
223 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now