aspose file tools*
The moose likes Beginning Java and the fly likes How to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to "flip" an array?" Watch "How to "flip" an array?" New topic
Author

How to "flip" an array?

Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Good day!

My task is to "flip" an array.
At first I thought that this was just a simple
array reversal but I was wrong.

This is what I currently have:

ab111111
cd111111
11111111
11111111
11111111

my task is to transform it to
this one with one click of a button:

11111111
11111111
11111111
cd111111
ab111111

I am sorry I cannot express myself in words that's
why I just described my inquiry in this way.

Is this possible?
Can anyone give me some ideas to solve this?

Thanks. God bless.

Darren
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Java is an "imperative" language; there are other languages like SQL which are "declarative." That means in SQL you can write SUM(something) and it adds it all up. In Java you can't (at least not yet), you have to write something1 + something2 + something3 + etc. Similarly you can't reverse an array in one operation. The correct way to do it is to find an Arrays.reverse() method, but if this is a class exercise you will get 0 marks for that!

It means that if you swap two values you have to use a holding value. Like this
Suggest you set up a for-loop which counts half the length of your array, then swap array[i] for array[array.length - i - 1].
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Hello there!

this is not a class exercise.
As of the moment I am thinking if I can
come up with a particular formula to make my array flip.
Have tried to google it but doesn't return anything valuable yet.
Still trying to find some solution.
Anyway, thanks for your reply.

God bless you.
[ January 09, 2008: Message edited by: Darren Alexandria ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
I have already told you to set up a for-loop for half the length of your array and then swap the values at i and at length - i - 1. That should be easy enough without having to look for solutions on Google.
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
I am so sorry, I happened to not understand your point.
My logic is not seem to working.
I will try what you suggested and apply it to my program.

Thank you very much.

God bless.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
If I gave you any more hints I would have to give you the entire code!
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Yes, I understand you and that would not be helpful
at all because I should learn to make it by myself.
And that is exactly what I would do.
Please don't be angry. Okie? I did not mean anything.
It's just that my logic is not yet that developed. =)

God bless you.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Originally posted by Darren Alexandria:
Yes, I understand you and that would not be helpful
at all because I should learn to make it by myself.
And that is exactly what I would do.



That's great!


Please don't be angry. Okie? I did not mean anything.


No one is angry here He is just trying to make you understand that he had told almost everything except the java code for the same

Good luck


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Originally posted by Raghavan Muthu:


No one is angry here He is just trying to make you understand that he had told almost everything except the java code for the same

Good luck


Yeah, I understand.
Well, I would just like to be polite and admit that I am having some
hard time on this. Just had an experience on the past in another
forum wherein I just asked a simple question then there are a couple
of veteran members of that forum said some harsh words on me. For
a reason which I don't know up to now. Even including the race issue.
I am not sure if they are racist but I did left the forum which I became regular member for 2 years. I even had the chance to answer other newbies question.

I ask questions (even though my question seemed so stupid to others)
because I really, really don't know what it is. I am not sure if
it is feasible to do or not.

At the middle of last year, I happened to come across this forum.
And as of the moment, I am very very happy to be a "ka-rancho" (this is
in my own native language) which means co-rancher in JavaRanch.
I often apologize because I want to show respect and appreciation to
those members who take their time to answer and think about my questions.
This forum has helped me a lot.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Thank you. But none of us thinks you have been disrespectful; we have all had difficulties ourselves and know what it feels like. We don't think your questions are stupid, and we are happy to help.

But, you have already realised that simply giving an answer (as happened on your other post, but I think you were given the answer to a question you didn't actually ask) doesn't help you. We try our hardest to guide without actually giving an answer.
Lukasz Bajzel
Greenhorn

Joined: Dec 03, 2007
Posts: 26
You have more than one option here
1. As mentioned in some of the replies you can either loop through the array and create a new array, with elements inserted in the reverse order
2. Create a list from the array and reverse it using the Collections class
List list = Arrays.asList(your array);
Collections.reverse(list);

Now if you want to sort elements, create a comparator or Collator and use code similar to below.


Below is an example of using Comparator

import java.util.*;

Comparator stringComp = new Comparator(){
public int compare(Object o1, Object o2){
return ((String)o1).compare( ((String)o2));
}
};

//Use the code below to test it.
List wordsList = new ArrayList(){{
add("Andy");
add("Amanda");
add("Alabama");
add("Abey");
}};

Collections.sort(wordsList,stringComp);
for(Iterator itr =wordsList.iterator(); itr.hasNext(); ){
System.out.println(itr.next());
}

Now the above example works fine in most of the cases. String compareTo() and compare() method of the Comparator class uses the character encoding defined in Unicode encoding. equls() method looks for character equality. Fair enough right? Not always! Think globally ... In some languages, the sequence by the Unicode encoding doesn't match the dictionary order. So what is the alternative. Use java.text.Collator.

import java.text.*;
Collator c = Collator.getInstance(Locale.ITALY);
int result = c.compare("string1", "string2");

Now, if we combine the the power of both Comparator and Collator, we easily get a mechanism that can compare a collection of Strings that works independent of the language encoding.

import java.text.Collator;
import java.util.*;

final Collator collator = Collator.getInstance(); //default locale
Comparator advancedComp = new Comparator(){
public int compare(Object o1, Object o2){
return collator.compare( ((String)o1), ((String)o2));
}
};

//Test the code
Collections.sort(wordsList,advancedComp);
for(Iterator itr =wordsList.iterator(); itr.hasNext(); ){
System.out.println(itr.next());
//gives the same result in example above for default locale
}

Hope this helps

Sincerly
www.javaadvice.com team
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
I agree with you, at some point giving out the exact answers temporarily helps but in the long run, you'll realize that it didn't help you that much especially if you are an aspiring programmer. Just so happy that people here are nicer than I thought.

Hi Lukasz Bajzel,

Thanks for your idea. I will study it then try to incorporate it with what
I am doing right now.

Thanks to all.
God bless everyone.

Darren
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
There are much simpler possible solutions than that.
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Good day!

Okay, so I will just try to think of other simpler ways to do this task.
I will try to experiment.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to "flip" an array?