Results 1 to 5 of 5

Thread: Array problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Jutland, Denmark
    Posts
    71

    Array problem

    In the main I have a Array, how can i use the data from this array in a sub

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Do you mean somthing like this?
    Code:
        class ArrayExample{ 
          public static void main(String[] args){
    
           String[] s = {"Hello","Whats's","up?"};
           printStrings(s); 
          
           
           }
          static void printStrings(String[] s){
            for(int i = 0; i < s.length; ++i){ 
               System.out.println(s[i]); 
            }
          } 
       }

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Some things that i would like to point out. Java is a pass by value language. Not a pass by refrence. First let me explain pass by refrence. When a variable is passed to a method a copy of that variable is passed. If the method modifies it's parameters those modifications are visible when the method returns.

    Java does not do this. It is a pass by value language. For instance. The following code illustrates this point.

    Code:
    class Example{ 
          public static void main(String[] args){
    
           int i = 10; 
           printInt(i); 
           System.out.println(i);  // prints 10
                   
           }
          static void printInt(int i){
              System.out.println(i); // prints 10
              i = 25;
              
          } 
       }
    However when a refrence type is involved, the valus is passed by refrence. This is not the same as "pass-by-refrence". If java were a pass by refrence language, when a refrence type was passed to a method, it would be passed as a refrence to the refrence.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    If you notice in this block of code. When an array is passed to a method. It's contents are modified when the method returns.
    Code:
     class ArrayExample{ 
          public static void main(String[] args){
    
           String[] s = {"Hello","Whats's","up?"};
           printStrings(s); 
           for(int i = 0; i < s.length; ++i){         
               System.out.println(s[i]);
             }
           
           }
          static void printStrings(String[] s){
             for(int i = 0; i < s.length; ++i){         
               System.out.println(s[i]);
             }
             s[2] = "down"; 
          } 
       }

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Heres another example. Even though we are modifying the contents of the variable i and returning that value from the method the contents of the variable were never changed.

    Code:
        class Example{ 
          public static void main(String[] args){
    
           int i = 10;
           int k = 0;  
           k = printInt(i); 
           System.out.println(k);  // prints 10
                   
           }
          static int printInt(int i){
              System.out.println(i); // prints 10
              i = 25;
              return i; 
          } 
       }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width