Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Performing a swap within an array

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try some thing like this :

[deleted]
[ October 27, 2006: Message edited by: fred rosenberger ]
 
lowercase baba
Posts: 13063
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My mistake.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic