Results 1 to 5 of 5
  1. #1
    glchau is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Post Cannot swap elements in ArrayList

    Hi all,

    When I run the above code, bugs occur:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.set(ArrayList.java:337)
    at java.util.Collections.swap(Collections.java:474)
    at draft.RuleOfTranslate.testPOS(RuleOfTranslate.java :12)
    at draft.Main.main(Main.java:17)
    Java Result: 1

    Java Code:
     public void testPOS(ArrayList<String> vie, ArrayList<String>tag){
            for (int i=0; i<=tag.size();i++){
                if (tag.get(i).equals("A")){
                    if (tag.get(i+1).equals("R")){
                               Collections.swap(tag, i, i+1);
                    }
                }
            }
           
        }
    tag=[N, N, A, R];

    Please help me!!!!!!!!!!!

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The indexes of the elements are this:
    Java Code:
    N, N, A, R
    0, 1, 2, 3
    Take a look at what's going on with your loop variable i, and you should track down the problem.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Agrees with Moonchile.

    When confronted with something like this, I sometimes add a bunch of println statements in the code to show me what the variables are at certain time points in my code. For e.g.,
    Java Code:
      public static void testPOS(ArrayList<String> vie, ArrayList<String> tag) {
        for (int i = 0; i <= tag.size(); i++) {
          System.out.println("i is: " + i);
          System.out.println("tag.get(i) is: " + tag.get(i));
          System.out.println("tag.get(i + 1) is: " + tag.get(i + 1));
          
          if (tag.get(i).equals("A")) {
            if (tag.get(i + 1).equals("R")) {
              Collections.swap(tag, i, i + 1);
            }
          }
        }

  4. #4
    glchau is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for your help. It works.
    However, when I want to print the ArrayList again, I cannot print it.

    Java Code:
     public void testPOS(ArrayList<String> vie, ArrayList<String> tag) {
        for (int i = 0; i <= tag.size(); i++) {
          System.out.println("i is: " + i);
          System.out.println("tag.get(i) is: " + tag.get(i));
          System.out.println("tag.get(i + 1) is: " + tag.get(i + 1));
    
          if (tag.get(i).equals("A")) {
            if (tag.get(i + 1).equals("R")) {
              Collections.swap(tag, i, i + 1);
                System.out.println("sucess");
            }
          }
        }
       [B]  System.out.println(tag);[/B]
      }

  5. #5
    glchau is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. swap method
    By leavearth in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 06:59 PM
  2. Replies: 5
    Last Post: 01-18-2010, 07:00 AM
  3. How to swap to images on clicks
    By ntagrafix in forum AWT / Swing
    Replies: 2
    Last Post: 12-03-2009, 08:13 PM
  4. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM
  5. how to swap 2 numbers
    By mary in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 05:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •