Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Performing a swap within an array  RSS feed

 
Andy Rayburn
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I was wondering if someone could help. First let me start by saying that this problem is for a class. I have looked through some past posts but I am not sure I am looking for the right thing.

The problem is I need to make a code that will swap the first and last characters in an array. But I have to figure out how to keep it simple so that no matter how big the array gets it will perform the same way. Here is what I have so far.

public class Lab21C {
public static void main(String[] args) {
char[] table = {'a', 'b', 'c', 'd'};

If someone could get me going in the right direction I would really appreciate it.

Thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store the value of the first element in a temporary variable.
Set the value of the first element to the value of the last element.
Set the value of the last element to the value of the temporary variable.

The index of the last element is table.length - 1.
 
paramu iyer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try some thing like this :

[deleted]
[ October 27, 2006: Message edited by: fred rosenberger ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't just post the answer. The Javaranch tries to help people learn how to code. Simply providing the solution in code does not help anyone.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do peoples homework for them. The idea is to give them hints and suggestions so that they can figure it out for themselves.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The index of the last element is table.length - 1.

There is nothing to suggest that would be the general case. For instance, if you have an array:

char[] str = new char[10];
str[0] = 'a';
str[1] = 'b';

The index of the last element will not be str.length-1. It sounds like the op needs to use a for-loop to find the index position of the last element in the array.
[ October 27, 2006: Message edited by: sven studde ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sven studde:

There is nothing to suggest that would be the general case.


Except for the fact that, by definition, that's how arrays work. You're assuming that he might have an array larger than needed to hold the characters of interest, plus perhaps an integer count of those characters. If, in fact, that was what he had, I suspect that he'd let us know. In any event, '0' is a perfectly valid character; there's no such thing as an "empty" char array element in Java.
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0 will always be the start of the array as the first element.

And to get them last element you can say something like.



That will return the length of the array, which will be the last element.

Now you just have to figure out how to swap them.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Foy:

That will return the length of the array, which will be the last element.


Considering how to find the last element was already accurately stated multiple times I'm not sure it's necessary to rehash it. Regardless, the length of the array isn't the last element, it's the length of the array minus one.
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, length is a field of the array class, not a method, so it's myArray.length, not myArray.length().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!