?
Solved

java: simple array question

Posted on 2011-02-11
14
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

770 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