11 Replies - 3931 Views - Last Post: 26 April 2011 - 09:05 AM Rate Topic: -----

#1 CorruptionInc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-March 11

Reversing String Elements of an Array List

Posted 25 April 2011 - 10:54 PM

I'm back for more friendly advice! So, I have to read an input file and reverse each line while keeping the lines in the same order, then output to the same file. I thought the best way to do this was to extract each line into an Array List, which I've done... But I can't figure out how to reverse the elements of the array list. If I were just reversing the order of the array list it would be easy... Any help is greatly appreciated, I have my input and output set... just missing the reverse method...Any help is GREATLY appreciated.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

public class ReverseLines {

  public static void main(String[] args) {
    try {
      BufferedReader input = new BufferedReader(new FileReader("java.txt"));
      ArrayList list = new ArrayList();
      String line;
      while ((line = input.readLine()) != null) {
        list.add(line);
      }
      input.close();


      PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("java.txt")));
      for (Iterator i = list.iterator(); i.hasNext();)/> {
        output.println((String) i.next());
      }
      output.close();
    } 
    catch (IOException e) {
      System.out.println(e);
    }
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Reversing String Elements of an Array List

#2 Kurplepush  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 08-March 10

Re: Reversing String Elements of an Array List

Posted 25 April 2011 - 11:07 PM

Instead of using an array list trying using a dequeue to reverse the characters of each line by passing them in using enqueue() method then remove them from the back using removelast() method.
Was This Post Helpful? 0
  • +
  • -

#3 CorruptionInc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-March 11

Re: Reversing String Elements of an Array List

Posted 25 April 2011 - 11:23 PM

View PostKurplepush, on 25 April 2011 - 11:07 PM, said:

Instead of using an array list trying using a dequeue to reverse the characters of each line by passing them in using enqueue() method then remove them from the back using removelast() method.


I'm not familiar at all with those methods... I'll try to look them up... I really thought I was on the right track though.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,357
  • Joined: 19-March 11

Re: Reversing String Elements of an Array List

Posted 25 April 2011 - 11:23 PM

To reverse an arraylist, presumably, you want to take the last element, remove it from the arraylist, and print it, and repeat until the list is empty. Lucky you, that's pretty easy to do if you look at the methods of ArrayList.
Was This Post Helpful? 0
  • +
  • -

#5 CorruptionInc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-March 11

Re: Reversing String Elements of an Array List

Posted 25 April 2011 - 11:35 PM

View Postjon.kiparsky, on 25 April 2011 - 11:23 PM, said:

To reverse an arraylist, presumably, you want to take the last element, remove it from the arraylist, and print it, and repeat until the list is empty. Lucky you, that's pretty easy to do if you look at the methods of ArrayList.


Thanks for the advice, but I'm not trying to reverse the array list... collection.reverse would do that... I need to reverse each line of the array while keeping the lines in the same order, and store them into an output file.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,357
  • Joined: 19-March 11

Re: Reversing String Elements of an Array List

Posted 25 April 2011 - 11:38 PM

Lucky you, same logic applies. Ain't life beautiful?
(honestly, for a String, I'd just count back instead of trying to pull off the final character each time - go to the end of the string and walk back until you hit zero)
Was This Post Helpful? 1
  • +
  • -

#7 CorruptionInc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-March 11

Re: Reversing String Elements of an Array List

Posted 25 April 2011 - 11:54 PM

I understand how to reverse a single string... what I do not understand, is how to reverse a string, and keep it in the array list so that I can put it into the output file.
Was This Post Helpful? 0
  • +
  • -

#8 CorruptionInc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-March 11

Re: Reversing String Elements of an Array List

Posted 26 April 2011 - 12:12 AM

Thanks for all the help... I just modified my code to take each line (string), reverse it with stringbuffer and add THAT to the arraylist. Works perfectly.
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2044
  • View blog
  • Posts: 4,224
  • Joined: 11-December 07

Re: Reversing String Elements of an Array List

Posted 26 April 2011 - 04:41 AM

There is a method called set() in the ArrayList class that lets you set the value at any index.
the set() method on the API elsewhere ion the tubes.

All you need to do is get() the line, reverse it, set() it back into the ArrayList.

This post has been edited by cfoley: 26 April 2011 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10648
  • View blog
  • Posts: 39,544
  • Joined: 27-December 08

Re: Reversing String Elements of an Array List

Posted 26 April 2011 - 08:11 AM

I would just store an ArrayList<StringBuilder>, as you can use the StringBuilder reverse() method.

ArrayList<StringBuilder> list = new ArrayList<StringBuilder>();
list.add(new StringBuilder("123"));
list.add(new StringBuilder("456"));
list.add(new StringBuilder("789"));

for(int i = 0; i < list.size(); i++)
  list.get(i).reverse();


Was This Post Helpful? 3
  • +
  • -

#11 CorruptionInc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-March 11

Re: Reversing String Elements of an Array List

Posted 26 April 2011 - 09:01 AM

View Postmacosxnerd101, on 26 April 2011 - 08:11 AM, said:

I would just store an ArrayList<StringBuilder>, as you can use the StringBuilder reverse() method.

ArrayList<StringBuilder> list = new ArrayList<StringBuilder>();
list.add(new StringBuilder("123"));
list.add(new StringBuilder("456"));
list.add(new StringBuilder("789"));

for(int i = 0; i < list.size(); i++)
  list.get(i).reverse();



Thanks... here's what I actually did....
while ((line = input.readLine()) != null) {
    	  StringBuilder sb = new StringBuilder(line);
          sb.reverse();
          list.add(sb.toString());

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10648
  • View blog
  • Posts: 39,544
  • Joined: 27-December 08

Re: Reversing String Elements of an Array List

Posted 26 April 2011 - 09:05 AM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1