Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with understanding arrays

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with understanding arrays

    If I had a string array such as array[1]="abcd". Is it possible to interate through the elements of that, so for example if I wanted to shift the positions to make it dabc. So far I only have come across problems where an array, for examples args who has 3 elements args[0]=1, args[1]=2 and args[2]=3 and I would use a FOR loop to do that:


    int b=3;
    String[] args= new char[b];
     
    for(int i=0; i < args.length; i++)
    {
       //do something
    }

    Pretty much what I'm wondering is, can you iterate through a string in one element of an array using a loop?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with understanding arrays

    See the API for the String class. Lots of methods that you might find useful (for instance the toCharArray method).
    String (Java Platform SE 7 )

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with understanding arrays

    Hi I tried to just make a sample code for me to understand how to change the elements to containing a different letter but it didn't quite work out. I am trying to shift 1 position down, so b to the first element, c to second element and etc. I was thinking of using a nested for loop but then I got even more confused.

     String test="abcd";
     char[] array = test.toCharArray();
     
                     for(int i=1; i < array.length; i++)
    		 {
    			 array[i]=array[i-1];
    		 }
     
    		         System.out.print(array[0]);
    		         System.out.print(array[1]);
    		         System.out.print(array[2]);
    		         System.out.print(array[3]);


    --- Update ---

    Ah nevermind. I think I have got the hang of it.

Similar Threads

  1. Need help understanding Java
    By Jafke104 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2013, 10:52 PM
  2. Helping Understanding???
    By SuperDad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2012, 06:53 PM
  3. Not really Understanding JPanel..
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 7
    Last Post: January 1st, 2012, 05:48 AM
  4. Need Help Understanding Where I went wrong
    By basedoverlord12 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 1st, 2011, 06:40 PM
  5. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 PM