Solved

java: simple array question

Posted on 2011-02-11
14
213 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

706 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

20 Experts available now in Live!

Get 1:1 Help Now