Solved

java: simple array question

Posted on 2011-02-11
14
238 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
fibonacci ten numbers 4 45
java stored proc example 9 44
sql import cannot be resolved jsp 3 47
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

856 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