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: Can A String array be passed as a parameter to a method that takes object array as argument?

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can A String array be passed as a parameter to a method that takes object array as argument?

    public class Knuth { 
     
        private Knuth() { }
     
        public static void shuffle(Object[] a){
            int n = a.length;
            for (int i = 0; i < n; i++) {
                // choose index uniformly in [0, i]
                int r = (int) (Math.random() * (i + 1));
                Object swap = a[r];
                a[r] = a[i];
                a[i] = swap;
            }
        }
     
     
        public static void shuffleAlternate(Object[] a) {
            int n = a.length;
            for (int i = 0; i < n; i++) {
                // choose index uniformly in [i, n-1]
                int r = i + (int) (Math.random() * (n - i));
                Object swap = a[r];
                a[r] = a[i];
                a[i] = swap;
            }
        }
     
     
        public static void main(String[] args) {
     
            // read in the data
            String[] a = StdIn.readAllStrings();
     
            // shuffle the array
            Knuth.shuffle(a);
     
            // print results.
            for (int i = 0; i < a.length; i++)
                StdOut.println(a[i]);
        }
    }
    Last edited by Md Abdullah Shuvo; March 24th, 2021 at 02:52 AM. Reason: I got the wrong category as I am new in this group. I want to delete this post or change the category.

  2. #2
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can A String array be passed as a parameter to a method that takes object array as argument?

    public class Knuth {

    private Knuth() { }

    public static void shuffle(Object[] a)
    {
    int n = a.length;
    for (int i = 0; i < n; i++) {
    // choose index uniformly in [0, i]
    int r = (int) (Math.random() * (i + 1));
    Object swap = a[r];
    a[r] = a[i];
    a[i] = swap;
    }
    }


    public static void shuffleAlternate(Object[] a) {
    int n = a.length;
    for (int i = 0; i < n; i++) {
    // choose index uniformly in [i, n-1]
    int r = i + (int) (Math.random() * (n - i));
    Object swap = a[r];
    a[r] = a[i];
    a[i] = swap;
    }
    }


    public static void main(String[] args) {

    // read in the data
    String[] a = StdIn.readAllStrings();
     
            // shuffle the array
            Knuth.shuffle(a);

    // print results.
    for (int i = 0; i < a.length; i++)
    StdOut.println(a[i]);
    }
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can A String array be passed as a parameter to a method that takes object array as argument?

    What happens when you compile and execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Passing an array as a parameter to a method - all elements after [0] generate an error.
    By Krotus in forum Object Oriented Programming
    Replies: 5
    Last Post: February 17th, 2019, 02:51 AM
  2. Replies: 6
    Last Post: February 9th, 2019, 12:48 PM
  3. [SOLVED] Parameterized array type as a method parameter
    By Lediator in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 1st, 2014, 01:08 AM
  4. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  5. Finding a substring from an array within a string argument
    By sitruz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2011, 10:33 AM

Tags for this Thread