Results 1 to 6 of 6
- 06-03-2011, 03:48 AM #1Member
- Join Date
- Jun 2011
- Rep Power
Problems with empty arrays (Putting
I've been trying to find a way to put the first character of a string to the end of the string. What I'm doing is the following:
- Create an char array with the single letters in each slot.
- Taking the first character and putting it into the last array number + 1.
- making the first array = null; (and this is where the problem lies)
When I make the array = null and System.out.print(array); it gives me an error because its empty. I also tried doing array = " "; and still gives me the same error.
I don't have the script with me its at school but thats how I'm doing it sorry I can't get the error message.
Is there a way to destrow the first array? so I don't need it, or possibly make a loop that shifts everything over -1? But that seems long a redundant.
Any help is greatly appreciated!
- 06-03-2011, 04:18 AM #2
Arrays do not support shifting.
Try using a loop, setting index i to i+1. Stop when i+1 is the last element. Then you can set the last element to the save char from the beginning.
- 06-03-2011, 04:20 AM #3Senior Member
- Join Date
- Feb 2011
- Georgia, USA
- Rep Power
"Taking the first character and putting it into the last array number + 1."
This will cause an IndexOutofBounds exception because as you are saying you are inserting it into the last array index + 1.
Unless there is a reason you are not using the substring String function I would take a look at that.
String (Java 2 Platform SE v1.4.2), int)
I think the pseudo code for what you are trying to accomplish with the method you are attempting above would be something like this
String to char array Store index 0 (first char) to temp variable Loop char array from index 1 to last index retrieve current index of char array and insert into (current index - 1) End Loop insert temp variable (first char) to last index of char array convert char array to String -- Return or Print String
- 06-03-2011, 04:29 AM #4
Use a container that allows the removing and adding of elements. See Vector or ArrayList
Arrays require moving ALL of the elements.
- 06-03-2011, 05:16 AM #5
I'd just use substring. It would make it much easier.
- 06-03-2011, 06:10 AM #6
- By Desmond in forum New To JavaReplies: 6Last Post: 07-27-2010, 05:54 PM
- By doha786 in forum New To JavaReplies: 0Last Post: 04-05-2010, 07:06 AM
- By circuspeanuts in forum New To JavaReplies: 11Last Post: 04-06-2009, 08:08 PM
- By Cymro in forum New To JavaReplies: 4Last Post: 02-01-2008, 07:54 PM
- By Marcus in forum New To JavaReplies: 2Last Post: 07-04-2007, 09:10 AM