Results 1 to 5 of 5
  1. #1
    holy.water is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default Problem with shifting array elements

    Hi,

    I need help in arrays.

    Let's say i have array that contains DD,GG and JJ.

    arr="DD","GG","JJ"

    if i want to insert FF in between DD and GG, i would need to shift GG and JJ one space back to have this final array.

    arr="DD","FF","GG","JJ"

    I have written this code but i am not sure why the final results become this way.

    arr="DD","FF","JJ","JJ"

    Java Code:
      for (int i = arr.length +1; i > 1; i--) {
            arr[i] = arr[i-1];
           }
    Pls advise. Thank You.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Add a print statement in there that specifies what element you're moving to which index and what the total array looks like each iteration, and you'll see what's going on.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    holy.water is offline Member
    Join Date
    Feb 2011
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    for (int i = arr.length +1; i > 1; i--) {
            arr[i] = arr[i-1];
    System.out.println(arr[i]);
    System.out.println(arr[i-1]);
           }
    As in this way?

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by holy.water View Post
    Java Code:
    for (int i = arr.length +1; i > 1; i--) {
            arr[i] = arr[i-1];
    System.out.println(arr[i]);
    System.out.println(arr[i-1]);
           }
    As in this way?
    More like this:
    Java Code:
            for (int i = arr.length + 1; i > 1; i--) {
                System.out.println("Moving " + arr[i-1] + " to index " + i);
                arr[i] = arr[i-1];
                System.out.print("Array is now: " + arr[0]);
                for (int j = 1; j < arr.length; j++) {
                     System.out.print(", " + arr[j]);
                }
                System.out.println();
            }
    -Gary-

  5. #5
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    No, no, no--you do NOT want to manipulate arrays directly like this. Use a java.util.ArrayList instead. To wit:

    Java Code:
    List<String> list = new ArrayList<String>();
    list.add("DD");
    list.add("GG");
    list.add("JJ");
    
    // now add "HH" at the appropriate index. All other elements will shift as needed.
    list.add(1, "HH");
    
    // need it to be a regular array again?
    String[] arr = list.toArray();
    BTW, if you don't like the way that ArrayList is populated, you can always do a quick & dirty

    Java Code:
    String[] arr = new String[] { "DD", "GG", "JJ" }
    List<String> list = new ArrayList<String>(Arrays.asList(arr));
    (Why not just List<String> list = Arrays.asList(arr)? Because Arrays.asList() returns a non-modifiable list.)

Similar Threads

  1. Q about shifting data in an array
    By alihht in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 06:17 AM
  2. Shifting characters in array
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 10:19 PM
  3. [SOLVED] Shifting an array
    By VeasMKII in forum New To Java
    Replies: 2
    Last Post: 02-04-2009, 06:18 PM
  4. PROBLEM - calculating with array elements
    By ella in forum New To Java
    Replies: 13
    Last Post: 12-04-2008, 12:36 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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