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 5 of 5

Thread: ArrayList to String/outOfBounds

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default ArrayList to String/outOfBounds

    Hiya,

    I am trying to create a single String out of an array list.

    I am building up the code, but does anyone know why I get a arrayOutOfBounds for this

    protected ArrayList<Dice> allRounds = new ArrayList<Dice>();
    protected String[] roundInfo = new String[allRounds.size()];
     
    ....................................................................................................	
     
    public void newFin(Dice addRound) {
    			allRounds.add(addRound);
      		}
     
    		public String[] getInfoForAll) {
    			for (int i = 0; i<allRounds.size(); i++) {
    				roundInfo[i] = getInfo(allRounds.get(i));
    			}
     
    			return roundInfo;
     
    		}

    or does anyone know how to create a single String out of an array of Strings?

    Thanks,


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList to String/outOfBounds

    why do not use roundInfo.length instead of allRound.size()?? to create a single String out of an array of Strings just do astring+=array[i];

  3. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList to String/outOfBounds

    because it is in arrayList not an array? so array.length is wrong?

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: ArrayList to String/outOfBounds

    You are going to need to post something we can compile.

    Take a look at this and see if it will help you:

    import java.util.ArrayList;
     
    public class ArrayListString {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
        public static void main(String[] args) {
     
            ArrayList<String> al = new ArrayList<String>();
     
            // Fill ArrayList (index, String)
            al.add(0, "Java");
            al.add(1, "Programming");
            al.add(2, "Forums");
            al.add(3, ".com");
     
            // Convert ArrayList to Object array
            Object[] elements = al.toArray();
     
            String myString = "";
     
            // Print Object content
            for (int a = 0; a < elements.length; a++) {
                //System.out.println(elements[a]);
            	myString = myString + elements[a];
            }
     
            // New complete String
            System.out.println(myString);
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    Scotty (February 28th, 2011)

  6. #5
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList to String/outOfBounds

    Thank you!

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Outofbounds error
    By tendulkarfan4life in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 11:20 AM
  3. ArrayList<String> convert to lowercase
    By s_mehdi76 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2010, 09:05 PM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM