logical array related problem

F

focode

I am developing an application , and facing a situation which needs
to be sorted out .. the problem is as follows

their is two array 1 String[] a = null;
2 String[] b = null;

in a recursive function the index value or subscript value of array
'a' will remain fixed from 0 to 10 , and every times this recursive
function is called fresh new value of 'a' will come . i want to copy
all the values of 'a' into array 'b' with a condition that : every
time the fresh value which comes in 'a' gets copied in 'b' with
increased subscript value for example when the first value of a (index
value 0 to 10 ) comes it gets copied in b with subscript value 0 to
10 , next time when fresh value of 'a' comes it will get copied in b
[11 to 20] , and so on ...

in short

first run b[0 to 10] = a [0 to 10]
second run [11 to 20 = a[0 to10 ] // these values 'a' are new values,
not the values of the first run ;


please help me solve the problem

thanks and regards
Arunesh
 
F

focode

focode said:
I  am developing an application , and facing a situation which needs
to be sorted out .. the problem is as follows
their is two array 1 String[] a = null;
                         2 String[] b = null;
in a recursive function the index value or subscript value of array
'a' will remain fixed from 0 to 10 , and every times this recursive
function is called fresh new value of 'a' will come . i want to copy
all the values of 'a' into array 'b' with a condition that : every
time the fresh value which comes in 'a' gets copied in 'b' with
increased subscript value for example when the first value of a (index
value 0 to 10 ) comes it gets copied in b with subscript value 0 to
10 , next time when fresh value of 'a' comes it will get copied in b
[11 to 20] , and so on ...
first run b[0 to 10] = a [0 to 10]
second run [11 to 20 = a[0 to10 ] // these values 'a' are new values,
not the values of the first run ;
please  help me solve the problem

If you can write an unambiguous specification of what you're actually
looking for, a specific reply can be offered.  As things stand now, you
haven't described the method arguments or how these arrays related to
the arguments (whether they are arguments themselves, or somehow
affected by the arguments?), nor where the "new a" comes from, nor
whether the array "b" is the same for each call of the method, nor how
recursion even relates to this, etc.

Basically, we have no idea what is _actually_ going on here.  You've
provided a very vague, general description, and the only information
that can be offered based on that description is so basic (i.e. "just
increment an offset into the array 'b' by 10 each iteration"), it's very
hard to believe that's actually what you're asking for.

Furthermore, your question seems to have the form "I need help with
implementation detail X", while the best questions have the form "I need
help to achieve goal Y".  That is, surely you have some more fundamental
underlying goal that has led you to think that you need to copy array
contents in this way.  But all you're asking about here is that actual
implementation you've settled on.  You could probably get a LOT better
advice if you'd explain (concisely, precisely, and unambiguously!) what
the more fundamental underlying goal is.

Please ask the question in a better way.  That way, someone can actually
help you solve the problem.

Pete

Dear Pete , i regret that i was not able to describe my problem very
much .. actually their is not much to describe about it ...
i need the following functionality that is provided by the following
pseudo code :

String a[] = {"red","green","blue"};
String b[] = {"pink","sink","drink"};
array_push(a,b); // i need to implement this function ..
System.out.println(a[5]);

// output should be drink

i need a function similar to "array_push" in java .

thanks and reagrds
Arunesh
 
F

focode

focode said:
I  am developing an application , and facing a situation which needs
to be sorted out .. the problem is as follows
their is two array 1 String[] a = null;
                         2 String[] b = null;
in a recursive function the index value or subscript value of array
'a' will remain fixed from 0 to 10 , and every times this recursive
function is called fresh new value of 'a' will come . i want to copy
all the values of 'a' into array 'b' with a condition that : every
time the fresh value which comes in 'a' gets copied in 'b' with
increased subscript value for example when the first value of a (index
value 0 to 10 ) comes it gets copied in b with subscript value 0 to
10 , next time when fresh value of 'a' comes it will get copied in b
[11 to 20] , and so on ...
in short
first run b[0 to 10] = a [0 to 10]
second run [11 to 20 = a[0 to10 ] // these values 'a' are new values,
not the values of the first run ;
please  help me solve the problem
If you can write an unambiguous specification of what you're actually
looking for, a specific reply can be offered.  As things stand now, you
haven't described the method arguments or how these arrays related to
the arguments (whether they are arguments themselves, or somehow
affected by the arguments?), nor where the "new a" comes from, nor
whether the array "b" is the same for each call of the method, nor how
recursion even relates to this, etc.
Basically, we have no idea what is _actually_ going on here.  You've
provided a very vague, general description, and the only information
that can be offered based on that description is so basic (i.e. "just
increment an offset into the array 'b' by 10 each iteration"), it's very
hard to believe that's actually what you're asking for.
Furthermore, your question seems to have the form "I need help with
implementation detail X", while the best questions have the form "I need
help to achieve goal Y".  That is, surely you have some more fundamental
underlying goal that has led you to think that you need to copy array
contents in this way.  But all you're asking about here is that actual
implementation you've settled on.  You could probably get a LOT better
advice if you'd explain (concisely, precisely, and unambiguously!) what
the more fundamental underlying goal is.
Please ask the question in a better way.  That way, someone can actually
help you solve the problem.

Dear Pete ,  i regret that i was not able to describe my problem very
much .. actually their is not much to describe about it ...
i need the following functionality that is provided by the following
pseudo code :

String a[] = {"red","green","blue"};
String b[] = {"pink","sink","drink"};
array_push(a,b); // i need to implement this function ..
System.out.println(a[5]);

// output should be drink

i need a function similar to "array_push" in java .

thanks and reagrds
Arunesh


i have got a solution by which i can continue my work , the
functionality i wanted to implement is already implemented in java,

in java their is function called "arraycopy" that does this job .

thanks and regards
Arunesh
 
R

Roedy Green

in a recursive function the index value or subscript value of array
'a' will remain fixed from 0 to 10 , and every times this recursive
function is called fresh new value of 'a' will come . i want to copy
all the values of 'a' into array 'b' with a condition that : every
time the fresh value which comes in 'a' gets copied in 'b' with
increased subscript value for example when the first value of a (index
value 0 to 10 ) comes it gets copied in b with subscript value 0 to
10 , next time when fresh value of 'a' comes it will get copied in b
[11 to 20] , and so on ...

In what context does this need come up other than a highly artificial
homework assignment? See http://mindprod.com/jgloss/homework.html
 
R

Robert Klemme

focode wrote:
I am developing an application , and facing a situation which needs
to be sorted out .. the problem is as follows
their is two array 1 String[] a = null;
2 String[] b = null;
in a recursive function the index value or subscript value of array
'a' will remain fixed from 0 to 10 , and every times this recursive
function is called fresh new value of 'a' will come . i want to copy
all the values of 'a' into array 'b' with a condition that : every
time the fresh value which comes in 'a' gets copied in 'b' with
increased subscript value for example when the first value of a (index
value 0 to 10 ) comes it gets copied in b with subscript value 0 to
10 , next time when fresh value of 'a' comes it will get copied in b
[11 to 20] , and so on ...
in short
first run b[0 to 10] = a [0 to 10]
second run [11 to 20 = a[0 to10 ] // these values 'a' are new values,
not the values of the first run ;
please help me solve the problem
If you can write an unambiguous specification of what you're actually
looking for, a specific reply can be offered. As things stand now, you
haven't described the method arguments or how these arrays related to
the arguments (whether they are arguments themselves, or somehow
affected by the arguments?), nor where the "new a" comes from, nor
whether the array "b" is the same for each call of the method, nor how
recursion even relates to this, etc.
Basically, we have no idea what is _actually_ going on here. You've
provided a very vague, general description, and the only information
that can be offered based on that description is so basic (i.e. "just
increment an offset into the array 'b' by 10 each iteration"), it's very
hard to believe that's actually what you're asking for.
Furthermore, your question seems to have the form "I need help with
implementation detail X", while the best questions have the form "I need
help to achieve goal Y". That is, surely you have some more fundamental
underlying goal that has led you to think that you need to copy array
contents in this way. But all you're asking about here is that actual
implementation you've settled on. You could probably get a LOT better
advice if you'd explain (concisely, precisely, and unambiguously!) what
the more fundamental underlying goal is.
Please ask the question in a better way. That way, someone can actually
help you solve the problem.
Pete
Dear Pete , i regret that i was not able to describe my problem very
much .. actually their is not much to describe about it ...
i need the following functionality that is provided by the following
pseudo code :

String a[] = {"red","green","blue"};
String b[] = {"pink","sink","drink"};
array_push(a,b); // i need to implement this function ..
System.out.println(a[5]);

// output should be drink

i need a function similar to "array_push" in java .

It seems like you want to *concatenate* arrays.
i have got a solution by which i can continue my work , the
functionality i wanted to implement is already implemented in java,

in java their is function called "arraycopy" that does this job .

It is not exactly the same but you can make your life easier by changing
at least b to List<String>. With class Arrays you can then wrap a and
use addAll() on the target list. That way you do not have to deal with
the details of resizing b if there is no more space left for the next a.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html

Kind regards

robert
 
A

Arved Sandstrom

Roedy said:
in a recursive function the index value or subscript value of array
'a' will remain fixed from 0 to 10 , and every times this recursive
function is called fresh new value of 'a' will come . i want to copy
all the values of 'a' into array 'b' with a condition that : every
time the fresh value which comes in 'a' gets copied in 'b' with
increased subscript value for example when the first value of a (index
value 0 to 10 ) comes it gets copied in b with subscript value 0 to
10 , next time when fresh value of 'a' comes it will get copied in b
[11 to 20] , and so on ...

In what context does this need come up other than a highly artificial
homework assignment? See http://mindprod.com/jgloss/homework.html

Why homework? His original problem description was fuzzy but it appears
that he can do what he needs to do with arraycopy, he wasn't aware of
arraycopy originally, he eventually discovered arraycopy, and now he's
happy.

Robert's observations were also bang-on. arraycopy could be used but
it's not the best choice (probably).

AHS
 
M

Mike Schilling

Arved said:
Why homework? His original problem description was fuzzy but it
appears that he can do what he needs to do with arraycopy, he wasn't
aware of arraycopy originally, he eventually discovered arraycopy,
and now he's happy.

Whuich is odd, because if arraycopy didn't exist, it could be
recreated in about three lines of code (assuming, as here, that it's
needed only for arrays of objects).
 
A

Arved Sandstrom

Mike said:
Whuich is odd, because if arraycopy didn't exist, it could be
recreated in about three lines of code (assuming, as here, that it's
needed only for arrays of objects).

Given all the error checking
(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#arraycopy(java.lang.Object,
int, java.lang.Object, int, int)), maybe not 3 lines of code.

But I understand your point. The guy thought he'd have to write
something like this from scratch, and I'm not sure why the big deal.
Let's just hope he's not a lead programmer on an important project.

AHS
 
L

Lew

Arved said:
His original problem description was fuzzy but it appears
that he can do what he needs to do with arraycopy, he wasn't aware of
arraycopy originally, he eventually discovered arraycopy, and now he's
happy.

Robert's observations were also bang-on. arraycopy could be used but
it's not the best choice (probably).

"Best" is a situational judgment call, as you hint.

Other options Robert didn't yet mention include

java.io_OutputStream, e.g., java.io.ByteArrayOutputStream
java.nio.ByteBuffer
java.io.Serializable

They take varying amounts of different kinds of work to do variations of what
the OP described.
 
D

Dev

I  am developing an application , and facing a situation which needs
to be sorted out .. the problem is as follows

their is two array 1 String[] a = null;
                         2 String[] b = null;

in a recursive function the index value or subscript value of array
'a' will remain fixed from 0 to 10 , and every times this recursive
function is called fresh new value of 'a' will come . i want to copy
all the values of 'a' into array 'b' with a condition that : every
time the fresh value which comes in 'a' gets copied in 'b' with
increased subscript value for example when the first value of a (index
value 0 to 10 ) comes it gets copied in b with subscript value 0 to
10 , next time when fresh value of 'a' comes it will get copied in b
[11 to 20] , and so on ...

in short

first run b[0 to 10] = a [0 to 10]
second run [11 to 20 = a[0 to10 ] // these values 'a' are new values,
not the values of the first run ;

please  help me solve the problem

thanks and regards
Arunesh

use two loops one for a and other for b, but instead of using array
use arrayList which is resizable.

all the best
Sunil
 
R

Robert Klemme

"Best" is a situational judgment call, as you hint.

Absolutely. So far we have seen only a tiny glimpse of an algorithm
without any information about the purpose.
Other options Robert didn't yet mention include

java.io_OutputStream, e.g., java.io.ByteArrayOutputStream
java.nio.ByteBuffer
java.io.Serializable

They take varying amounts of different kinds of work to do variations of
what the OP described.

Since OP was talking about concatenating String arrays we then should
probably also mention StringBuffer, StringBuilder, Writer (especially
StringWriter), CharBuffer and probably a few more. :)

Kind regards

robert
 
F

focode

Absolutely.  So far we have seen only a tiny glimpse of an algorithm
without any information about the purpose.




Since OP was talking about concatenating String arrays we then should
probably also mention StringBuffer, StringBuilder, Writer (especially
StringWriter), CharBuffer and probably a few more. :)

Kind regards

        robert

Dear Dev, i am working on j2me platform , and j2me does not support
"arraylist" , my problem was not part of any home work (Roedy) , this
problem arrived because of unexpected larger values coming from
server , "Let's just hope he's not a lead programmer on an important
project"by"Arved Sandstrom" i don't know what "important project"
means to , for me every project is important.

by the way, i didn't find anything use full in over all discussion of
the problem
but yet i am thankfull for every body taking part in the discussion.

thanks and regards
focode
 
A

Arved Sandstrom

focode said:
Dear Dev, i am working on j2me platform , and j2me does not support
"arraylist" , my problem was not part of any home work (Roedy) , this
problem arrived because of unexpected larger values coming from
server , "Let's just hope he's not a lead programmer on an important
project"by"Arved Sandstrom" i don't know what "important project"
means to , for me every project is important.
[ SNIP ]

The latter is a great attitude - I wish every developer had it. As to my
somewhat gratuitous (and somewhat unnecessary) comment, don't take it
personally - I simply meant what I said. System.arraycopy is a
well-known method in a core class, and a lead developer on an important
(read, important to a client) project would have thought of it. Maybe
not used it, but thought of it.

AHS
 
A

Arne Vajhøj

Whuich is odd, because if arraycopy didn't exist, it could be
recreated in about three lines of code (assuming, as here, that it's
needed only for arrays of objects).

beginner != homework

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top