Results 1 to 10 of 10

Thread: Array help

  1. #1
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Question Array help

    I have a few questions:

    for this function I can't seem to get rid of the comma that is produced at the end of the array no matter what I do:

    public static void printArray(int[] arr)
    {
    for (int i=0; i<arr.length; i++)
    {
    System.out.print(arr[i] + ", ");
    }
    System.out.println();
    }


    and I need to write a function that checks whether an array has any duplicate numbers in it or not. And it should return true if and only if the input array has any duplicate numbers. But i have no clue how to do this. can someone help?

    If anyone could help that would be great, I'm sorry if they are ridiculous questions but I am relatively new to java programming. thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    hm how about
    Java Code:
    for (int i=0; i<arr.length; i++)
    {
        System.out.print(arr[i]);
        if (something -- you have to figure out what)
        {
             System.out.print(", ");
        }
        System.out.println();
    }

  3. #3
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    hm how about
    Java Code:
    for (int i=0; i<arr.length; i++)
    {
        System.out.print(arr[i]);
        if (something -- you have to figure out what)
        {
             System.out.print(", ");
        }
        System.out.println();
    }
    Thank You, that helped a lot, I finally got it to work.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Cool! Glad it helped.

  5. #5
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default

    yea, now i just need to figure out the duplicating number array thing. I am totally confused on that one, and don't even know where to start other than:

    public static void hasDuplicate(int[] arr)
    {
    for(int i = 0, i < arr.length; i++)
    {
    //not sure what goes here
    }
    //return something
    }


    and i am not even sure that is right, haha

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In very basis level you can do this.

    Java Code:
        public static void main(String[] args) {
            int[] arr = {1, 2, 3, 4, 5, 6, 7, 2};
            if(hasDuplicate(arr))
                System.out.println("Yes");
        }
    
        public static boolean hasDuplicate(int[] arr) {
            int temp = arr[0];
            for(int i = 0; i < arr.length; i++) {
                if(temp == arr[i])
                    return true;
            }
            return false;
        }

  7. #7
    Join Date
    Oct 2008
    Posts
    11
    Rep Power
    0

    Default

    thank you for your help
    Last edited by EternalSolitude; 10-27-2008 at 04:47 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    int temp = arr[0]; // prime search with first element
    for(int i = 0; i < arr.length; i++) {
    This will always find the first item
    Start i at 1 vs 0 to check against second and following

  9. #9
    andy_man's Avatar
    andy_man is offline Member
    Join Date
    Oct 2008
    Location
    Yerevan
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    In very basis level you can do this.

    Java Code:
        public static void main(String[] args) {
            int[] arr = {1, 2, 3, 4, 5, 6, 7, 2};
            if(hasDuplicate(arr))
                System.out.println("Yes");
        }
    
        public static boolean hasDuplicate(int[] arr) {
            int temp = arr[0];
            for(int i = 0; i < arr.length; i++) {
                if(temp == arr[i])
                    return true;
            }
            return false;
        }
    Yes, the code you've written, is only finding whether there is any element in an array which equals to the first element of array, and for general cases, it'll be like:

    Java Code:
    public static void main(String[] args) {
            int[] arr = {1, 2, 3, 4, 5, 6, 7, 2};
            if(hasDuplicate(arr))
                System.out.println("Yes");
        }
    
        public static boolean hasDuplicate(int[] arr) {
            for(int i = 0; i < arr.length; i++) {
                temp = arr[i];
                for(int j = 0; j < arr.length; j++){
                    if(i != j && temp == arr[i])
                        return true;
                }
            }
            return false;
        }
    TEAM = Together Everyone Achieves More :)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    This will always find the first item
    Start i at 1 vs 0 to check against second and following
    Ya, I've made a mistake there. Sorry about that thanks for the pointing me.

Similar Threads

  1. new array ?
    By seandingobat in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 07:31 PM
  2. help with an array.
    By f_the_cook in forum New To Java
    Replies: 7
    Last Post: 06-03-2008, 04:05 AM
  3. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:08 PM
  4. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 02:25 AM
  5. 2D array
    By bluekswing in forum New To Java
    Replies: 2
    Last Post: 01-15-2008, 05:57 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
  •