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

Thread: Copying Arrays

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    95
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Copying Arrays

    How do u copy all the elements in an array eg A into another array eg B?

    This is the question:
    An array A contains integers that first increase in value and then decrease in value,
    for example, 17 24 31 39 44 49 36 29 20 18 13
    It is unknown at which point the numbers start to decrease. Write efficient code to
    code to copy the numbers in A to another array B so that B is sorted in ascending
    order. Your code must take advantage of the way the numbers are arranged in A.

    This is my program:
     


    This is the error message:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at Quest30.CopyAndSortArray.main(CopyAndSortArray.jav a:16)
    Last edited by Java girl; April 13th, 2014 at 05:02 PM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Copying Arrays

    take a look in statement in your code for (j = lo + 1; j <= hi; j++) , your hi variable is 11, so last iteration of your loop will make your j variable equal to 11 since the condition is less than or equal. then you are attempting to access the element of B[j] which is similar to B[11], and that is where the ArrayIndexOutOfBoundsException occurs, the last element of array B can be access through B[10].

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    95
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Copying Arrays

    I replaced the 11 with 10, same problem :/

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Copying Arrays

    paste the error, and paste the line where the error exist.

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    95
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Copying Arrays

    System.out.printf("%d",B);

    Exception in thread "main" java.util.IllegalFormatConversionException: d != [I
    at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:4045)
    at java.util.Formatter$FormatSpecifier.printInteger(F ormatter.java:2748)
    at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2702)
    at java.util.Formatter.format(Formatter.java:2488)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at worksheet6Quest29.CopyAndSortArray.main(CopyAndSor tArray.java:28)

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Copying Arrays

    so your problem now is about index of the array.
    you cannot format an array object to decimal. try to replace it with System.out.printf("%d",B[index]);

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    95
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Copying Arrays

    It says index cannot be resolved to variable, do i have to declare index as 0?

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Copying Arrays

    The index that I declared is just a dummy variable, it is up to you on how you declare it

  9. #9
    Member
    Join Date
    Mar 2014
    Posts
    95
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Copying Arrays

    ok

Similar Threads

  1. question about copying and flipping arrays
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 7th, 2013, 08:32 PM
  2. Copying arrays from one class to another
    By hannah87 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2013, 02:54 PM
  3. Copying a File
    By ubiByte in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 19th, 2012, 07:55 PM
  4. Copying Objects
    By MethodMan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2011, 03:41 AM
  5. Copying Arrays
    By AnnexTrunks in forum What's Wrong With My Code?
    Replies: 30
    Last Post: October 24th, 2011, 11:04 PM