 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
dimo414 Guest
|
Posted: Tue Nov 14, 2006 6:24 am Post subject: Passing Unknown Number of Varaibles |
|
|
I have two methods, let's call them apple and orange.
apple's signature is: public String apple(int...args)
orange's signature is: public Int orange((int...args)
What I'm trying to do is get apple to call orange, with the same
parameters. Something like:
Public String apple(int...args)
{
/*
* Process the apple arguments
*/
orange(args[1], args[2], args[n]);
}
The problem is, I don't have the ability to change orange's
functionality or signature, and I need to do it this way (trust me,
I've tried others). Is there some syntax or methodology to pass an
unknown number of variables to orange? |
|
| Back to top |
|
 |
Knute Johnson Guest
|
Posted: Tue Nov 14, 2006 8:09 am Post subject: Re: Passing Unknown Number of Varaibles |
|
|
dimo414 wrote:
| Quote: | I have two methods, let's call them apple and orange.
apple's signature is: public String apple(int...args)
orange's signature is: public Int orange((int...args)
What I'm trying to do is get apple to call orange, with the same
parameters. Something like:
Public String apple(int...args)
{
/*
* Process the apple arguments
*/
orange(args[1], args[2], args[n]);
}
The problem is, I don't have the ability to change orange's
functionality or signature, and I need to do it this way (trust me,
I've tried others). Is there some syntax or methodology to pass an
unknown number of variables to orange?
|
public class test11 {
public static void main(String[] args) {
apple(1,2,3);
}
public static void orange(int... args) {
for (int i : args)
System.out.println(i);
}
public static void apple(int... args) {
// do something
orange(args);
}
}
--
Knute Johnson
email s/nospam/knute/ |
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Tue Nov 14, 2006 8:09 am Post subject: Re: Passing Unknown Number of Varaibles |
|
|
dimo414 wrote:
| Quote: | I have two methods, let's call them apple and orange.
|
Or let's not (is another alternative).
The problem with such abstractions is that often
the poster is assuming the method names (and
ultimate meanings and use) is not relevant, whereas
it often is.
| Quote: | apple's signature is: public String apple(int...args)
orange's signature is: public Int orange((int...args)
What I'm trying to do is get apple to call orange, with the same
parameters. Something like:
Public String apple(int...args)
{
/*
* Process the apple arguments
*/
orange(args[1], args[2], args[n]);
}
The problem is, I don't have the ability to change orange's
functionality or signature, and I need to do it this way (trust me,
I've tried others).
|
Again, given you have not yet solved the problem,
it is difficult to 'trust you'. Care to share the details,
so we can make suggestions that amount to more
than ..guessing?
| Quote: | ..Is there some syntax or methodology to pass an
unknown number of variables to orange?
|
orange(Object[] array)
orange(Vector elements)
...however, I do not think that is the real problem
here, since apples() can simply iterate the arguments
and call oranges() from the loop. (Just venturing
into 'guessing').
Andrew T. |
|
| Back to top |
|
 |
dimo414 Guest
|
Posted: Tue Nov 14, 2006 8:11 am Post subject: Re: Passing Unknown Number of Varaibles |
|
|
Andrew Thompson wrote:
| Quote: | dimo414 wrote:
I have two methods, let's call them apple and orange.
Or let's not (is another alternative).
The problem with such abstractions is that often
the poster is assuming the method names (and
ultimate meanings and use) is not relevant, whereas
it often is.
apple's signature is: public String apple(int...args)
orange's signature is: public Int orange((int...args)
What I'm trying to do is get apple to call orange, with the same
parameters. Something like:
Public String apple(int...args)
{
/*
* Process the apple arguments
*/
orange(args[1], args[2], args[n]);
}
The problem is, I don't have the ability to change orange's
functionality or signature, and I need to do it this way (trust me,
I've tried others).
Again, given you have not yet solved the problem,
it is difficult to 'trust you'. Care to share the details,
so we can make suggestions that amount to more
than ..guessing?
..Is there some syntax or methodology to pass an
unknown number of variables to orange?
orange(Object[] array)
orange(Vector elements)
..however, I do not think that is the real problem
here, since apples() can simply iterate the arguments
and call oranges() from the loop. (Just venturing
into 'guessing').
Andrew T.
|
I was hoping there would be some simple answer, so I wouldn't have to
get into specifics. I'm building a static class to replace the
Formatter class*. So my formatter method intitalizes an instance of
Formatter, calls the format method, and returns it. But I cant figure
out how to pass a variable number of methods to the Formatter's format
method.
*After I started this project, I discovered the String.format() method,
which seems to do exactly what I'm looking for. But I really want to
figure out a way to do this, and it's not being very friendly.
I've tried passsing the whole array (Illegal argument), just the first
value, updating the formating string, and looping through each variable
(missing arguments, apparently it doesn't just process the first % it
finds, it makes sure theres a match for each one), escaping all the
parentheses exept the first one, which is, I suppose, theoreticaly
possible. But the backslash itslelf has to be escaped, and that has to
be escaped, and bleh...
Any idea, now that I've explained it more? |
|
| Back to top |
|
 |
Knute Johnson Guest
|
Posted: Wed Nov 15, 2006 5:43 am Post subject: Re: Passing Unknown Number of Varaibles |
|
|
Ingo R. Homann wrote:
| Quote: | Hi,
I'm not sure, if I understand you correctly, but you know, that the
following is possible, do you?
public void orange(int...args){}
public void apple(int...args)
{
// process args[i]
orange(args);
}
Ciao,
Ingo
|
Apparently he doesn't like that answer even though it does exactly what
he asked for.
--
Knute Johnson
email s/nospam/knute/ |
|
| Back to top |
|
 |
trippy Guest
|
Posted: Wed Nov 15, 2006 8:02 am Post subject: Re: Passing Unknown Number of Varaibles |
|
|
In article <1163463864.320323.277610 (AT) m73g2000cwd (DOT) googlegroups.com>,
dimo414 took the hamburger meat, threw it on the grill, and I said "Oh
Wow"...
| Quote: | I have two methods, let's call them apple and orange.
apple's signature is: public String apple(int...args)
orange's signature is: public Int orange((int...args)
What I'm trying to do is get apple to call orange, with the same
parameters. Something like:
Public String apple(int...args)
{
/*
* Process the apple arguments
*/
orange(args[1], args[2], args[n]);
}
The problem is, I don't have the ability to change orange's
functionality or signature, and I need to do it this way (trust me,
I've tried others). Is there some syntax or methodology to pass an
unknown number of variables to orange?
|
Might want to try putting the variables in an arraylist then passing
the arraylist. Beyond that, dunno.
--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "All I Really Want" -- Alanis Morissette
"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."
-- Robert Redford "Spy Game" |
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Wed Nov 15, 2006 11:50 pm Post subject: Re: Passing Unknown Number of Varaibles |
|
|
"dimo414" <dimo414 (AT) gmail (DOT) com> writes:
| Quote: | Any idea, now that I've explained it more?
|
If two methods take the same number and types of arguments, you can
use reflection. |
|
| Back to top |
|
 |
dimo414 Guest
|
Posted: Thu Nov 16, 2006 3:01 pm Post subject: Re: Passing Unknown Number of Varaibles |
|
|
I can't change the way format() (or orange()) takes arguments. I can't
pass it an array, or an arrayList, I have to pass it a series of
primative types or String objects. So if we were to simplify the
question, how can I take an array of values, and pass each value to a
method?
| Quote: | Any idea, now that I've explained it more?
If two methods take the same number and types of arguments, you can
use reflection.
|
I'm sorry, I don't know what that is, could you elaborate? |
|
| Back to top |
|
 |
Ingo R. Homann Guest
|
Posted: Thu Nov 16, 2006 6:26 pm Post subject: Re: Passing Unknown Number of Varaibles |
|
|
Hi,
| Quote: | Any idea, now that I've explained it more?
|
What's wrong with my post from 14.11., 10:05?
Anyway, the following is no problem, either:
void bar(String... strings)
{
}
void foo()
{
String[] strings=...
bar(strings);
}
I am very sure that it is very easy, what you want to achieve! Post us a
snippet and I guess it can be answered!
Ciao,
ingo |
|
| Back to top |
|
 |
dimo414 Guest
|
Posted: Sat Nov 18, 2006 3:21 am Post subject: Re: Passing Unknown Number of Varaibles |
|
|
Ingo R. Homann wrote:
| Quote: | Hi,
Any idea, now that I've explained it more?
What's wrong with my post from 14.11., 10:05?
Anyway, the following is no problem, either:
void bar(String... strings)
{
}
void foo()
{
String[] strings=...
bar(strings);
}
I am very sure that it is very easy, what you want to achieve! Post us a
snippet and I guess it can be answered!
Ciao,
ingo
|
Like I said in my reply on Mon, Nov 13 2006 9:55 pm, I am making a
static version of the formatter class, and the format method of that
class will not accept an array. |
|
| Back to top |
|
 |
Knute Johnson Guest
|
Posted: Sat Nov 18, 2006 6:47 am Post subject: Re: Passing Unknown Number of Varaibles |
|
|
dimo414 wrote:
| Quote: |
Like I said in my reply on Mon, Nov 13 2006 9:55 pm, I am making a
static version of the formatter class, and the format method of that
class will not accept an array.
|
That's right, it takes a variable number of arguments not an array.
You asked
"I have two methods, let's call them apple and orange.
apple's signature is: public String apple(int...args)
orange's signature is: public Int orange((int...args)
What I'm trying to do is get apple to call orange, with the same
parameters. Something like:
Public String apple(int...args)
{
/*
* Process the apple arguments
*/
orange(args[1], args[2], args[n]);
}"
Several of us have shown you the answer to that question. See my answer
and Ingo's.
Are you really trying to pass a variable number of arguments to a method
that takes an array?
public class test11 {
public static void main(String[] args) {
apple(1,2,3);
}
public static void orange(int[] args) {
for (int i : args)
System.out.println(i);
}
public static void apple(int... args) {
int[] arr = new int[args.length];
int index = 0;
for (int i : args)
arr[index++] = i;
orange(args);
}
}
--
Knute Johnson
email s/nospam/knute/ |
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Sat Nov 18, 2006 4:03 pm Post subject: Re: Passing Unknown Number of Varaibles |
|
|
"dimo414" <dimo414 (AT) gmail (DOT) com> writes:
| Quote: | I'm sorry, I don't know what that is, could you elaborate?
|
Basically you take the Method object from a class and invoke it using
an array of values. See the javadocs for java.lang.Class.getMethod(),
java.lang.reflect.Method and related stuff. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|