Results 1 to 2 of 2
  1. #1
    nalinda is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default calling array from main

    can anyone tell me how to call this array from my main method?
    public static int[] randomArray (int num) {

    int[] array = new int[num];

    for (int i = 0; i<= array.length; i++) {
    array[num] = (int)(Math.random () * 365) + 1;
    }

    return array;
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class CallingFromMain {
        public static void main(String[] args) {
            int[] n = randomArray(5);
            print(n);
            CallingFromMain cfm = new CallingFromMain();
            int[] j = cfm.randomArray(3);
            print(j);
        }
    
        public static int[] randomArray (int num) {
            int[] array = new int[num];
    
            for (int i = 0; i < array.length; i++) {
                array[i] = (int)(Math.random() * 365) + 1;
            }
    
            return array;
        }
    
        private static void print(int[] array) {
            for(int j = 0; j < array.length; j++) {
                System.out.print(array[j]);
                if(j < array.length-1)
                    System.out.print(", ");
                else
                    System.out.println();
            }
        }
    }

Similar Threads

  1. Calling main method
    By eva in forum New To Java
    Replies: 7
    Last Post: 11-06-2009, 02:37 PM
  2. Calling a variable from main to another class
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 04:35 PM
  3. Replies: 1
    Last Post: 08-07-2007, 08:10 AM
  4. Replies: 1
    Last Post: 08-06-2007, 10:11 AM
  5. Problem with main
    By Hyder in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-28-2007, 11:21 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
  •