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

    Default printArray error

    Below is my code and I expect the result is
    array1 is <2, 3, 5, 7, 11, 13, 17, 19>
    array1 is <0, 3, 2, 7, 4, 13, 6, 19>

    but the result shows like:

    array1 is: <>
    array1 is <>

    Please advise. Thanks

    Java Code:
        
            public static void main(String[] args) {
            // TODO code application logic here
            int[] array1 = {2, 3, 5, 7, 11, 13, 17, 19};
            int[] array2;
            System.out.print("array1 is: ");
            printArray(array1);
            System.out.println();
    
            array2 = array1;
            array2[0] = 2;
            array2[1] = 3;
            array2[2] = 2;
            array2[3] = 7;
            array2[4] = 4;
            array2[5] = 13;
            array2[6] = 6;
            array2[7] = 19;
    
            System.out.print("array1 is ");
            printArray(array1);
            System.out.println();
        }
    
        public static void printArray(int[] array) {
            System.out.print('<');
            for (int i = 0; i < array.length; i++) {
            // print an element
    
            //Do the coding here
    
            // print a comma delimiter if not the last element
    
            //Do the coding here
            }
            System.out.print('>');
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default

    I don't know where that zero comes from in your second print; you are hiding something from us; in your actual code you showed us you're not printing anything except for those angular brackets.

    kind regards,

    Jos

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

    Default

    No, I don't hide anything. I don't know why it doesn't show up like my expectation and thus it's great that you can fix and adjust my program to produce as my expected result, so I can learn from it. Thanks

  4. #4
    ice_breaker is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    I just solved my error. Below is the complete code:

    Java Code:
    public static void main(String[] args) {
            // TODO code application logic here
            int[] array1 = {2, 3, 5, 7, 11, 13, 17, 19};
            int[] array2;
            System.out.print("array1 is: ");
            printArray(array1);
            System.out.println();
    
            array2 = array1;
            array2[0] = 2;
            array2[1] = 3;
            array2[2] = 2;
            array2[3] = 7;
            array2[4] = 4;
            array2[5] = 13;
            array2[6] = 6;
            array2[7] = 19;
    
            System.out.print("array1 is ");
            printArray(array1);
            System.out.println();
        }
    
        public static void printArray(int[] array) {
            System.out.print('<');
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i]);
                if(i <(array.length -1))
                {
                    System.out.print(", ");
                }
    
            }
            System.out.print('>');
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  2. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  3. Replies: 1
    Last Post: 10-26-2009, 05:38 PM
  4. Replies: 3
    Last Post: 01-23-2009, 09:53 AM
  5. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 05:28 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
  •