permaculture playing cards*
The moose likes Beginning Java and the fly likes Stuffing one array into another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stuffing one array into another" Watch "Stuffing one array into another" New topic
Author

Stuffing one array into another

Jason Batchelder
Ranch Hand

Joined: Jan 27, 2005
Posts: 37
I have a question about arrays. I have an array that I want to reverse the order on and then make into another. I can read the first array forwards and backwards no problem but for some reason when I go to put it into another array it doesn't work. The second array is still empty. I've got to be doing something rather ignorant but just can't see it. Can someone help please.

Here's the code that I wrote for the second array.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, "j" varies in this loop, but not "i", so that every assignment is of the same character. Furthermore, did you mean something like "str2[j] = str1[i]"? You're assigning one element of str1 to another element of the same array str1.

I don't want to give the code away, but you do definitely want the index for both arrays to change on each iteration of the loop.


[Jess in Action][AskingGoodQuestions]
Jason Batchelder
Ranch Hand

Joined: Jan 27, 2005
Posts: 37
As a matter of fact I did. Sorry about that, I was doing some testing (aka grasping for straws) and tried to get one to equal the same thing and forgot to change it back before I posted.
Jason Batchelder
Ranch Hand

Joined: Jan 27, 2005
Posts: 37
Here's the revised code. It still don't work but I think it's closer. It now gives me an out of bounds error.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

OK, well, let's see. A few questions:

- The first element of a "maxLength"-character String is 0. What is the index of the last character?

- Are str1 and str2 allocated at the same size?

- Are you sure "i" starts at 0?
Jason Batchelder
Ranch Hand

Joined: Jan 27, 2005
Posts: 37
That's one of the problems with only posting the code that doesn't work.

Here's the answers to your questions.

- The first element of a "maxLength"-character String is 0. What is the index of the last character?

The index of the last character is supposed to be index 0 of the first array. I'm not sure if I set it up right but that's where it's supposed to be.

- Are str1 and str2 allocated at the same size?

maxLength is set to s.lenght() for both of them. What ever that may be. I won't know how long of a string they put in until they do but it's not supposed to be more then 80 chars.

- Are you sure "i" starts at 0?

i does start at 0 I set it before it goes through the loop.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Jason Batchelder:

The index of the last character is supposed to be index 0 of the first array. I'm not sure if I set it up right but that's where it's supposed to be.


Hmmm. You didn't quite get me there.

Looking at your code, one might suspect the answer is "maxLength-2". Is that the correct answer?
Jason Batchelder
Ranch Hand

Joined: Jan 27, 2005
Posts: 37
No not really. This is what is supposed to happen. The for loop is supposed to take the last element in the first array and put it in the first spot of the second. Then the next to the last element in the first array is supposed to go into the next element of the second and so on. I probably didn't set it up right but that's what I want to happen.

How would you go about doing this? I'm not sure how to do it. This loop does display the first array backwards so I thought I was on the right track but maybe not.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jason Batchelder:
No not really. This is what is supposed to happen. The for loop is supposed to take the last element in the first array and put it in the first spot of the second...

I think what EFH is trying to point out is that you need to think more closely about the index for the last element. Let's use a concrete example to illustrate. Say you have an array with 5 elements in it. What is the index of the last element? What index would your code above use? Are these two values different? If so, what do you need to do to fix it?

As a small clue, look closely at your initial value for j. If you need to, add some System.out.println() statements to see what the value of each variable is set to during execution of your program.

HTH

Layne


Java API Documentation
The Java Tutorial
Jason Batchelder
Ranch Hand

Joined: Jan 27, 2005
Posts: 37
Yes I know. I've tried all that. I've only been working on this for 3 weeks now. I've had println everywhere and it does go through the loop the correct number of times, displays the correct letter in the correct order, etc. The only thing I can't get it to do is add the stupid element to the second array.

This is the only way I can get it to work even this much. The other ways I've tried I can't even compile for one reason or another. I'm at my wits end with this thing. PLEASE HELP!!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

We're really trying to help... a lot of times, just by answering what seems like a silly question, you can have a flash of insight. But let's try another tack. Pictures always help me in tough situations.




So if you look at these two arrays, you see that element 4 from the first array goes into element 0 of the second; element 3 from the first goes into element 1 of the second; and generally, element length-j of the first goes into element j of the second. So that suggests that i equals "length - j".

So now you step over the elements of the second array, one at a time, from 0 up to length-1. And for each element, you compute the index to use from the first array, read the entry, and put it into the element you're visiting in the second.

Make sense? Anything more and I'd be writing the code for you.
Jason Batchelder
Ranch Hand

Joined: Jan 27, 2005
Posts: 37
Sorry must have been the brain injury I recieved in my car crash last month. (Yes, I really did receive one.) I finally got what you all were saying. Anyway I got that done now on to the next step. Comparing them. Thanks for putting up with me.
Paulo Lima
Greenhorn

Joined: Apr 04, 2002
Posts: 10
Hi Jason,

I suggest you to use Collections to do the "Reverse" work.

You can create a List from an Array. Reverse the List, then create a new Array from the Reversed List.

See this sample code:

import java.util.*;

public class Test {
public static void main(String[] args) {
String[] str1 = {"AA", "BB", "CC", "DD", "EE"};
// Converting an Array to a Collection
List list = Arrays.asList(str1);
// Reversing a Collection
Collections.reverse(list);
// Just to see reversed values
System.out.println(list);

// Create an array containing the elements in a list
Object[] objectArray = list.toArray();
String[] str2 = (String[])list.toArray(new String[list.size()]);
for (int i=0; i < str2.length; i++) {
System.out.println(str2[i]);
}
}
}


Regards,

Paulo Lima.
 
Don't get me started about those stupid light bulbs.
 
subject: Stuffing one array into another
 
Similar Threads
Insert into String
String Input and Bubble Sort
writing record to database
Substring Challenge/Console input error
equals ?????