1 2 3 Previous Next 72 Replies Latest reply: Jun 4, 2007 4:01 PM by 807600 RSS

    Have a method return an array

    807600
      I am designing a program that factors several numbers. I would like to have the "factoring" method as a subroutine of a main method with all the I/O stuff, to keep things organized. I was wondering what the syntax is for a method that returns an array (which would hold the factors).

      Would it be like this: "Array[][] returnfactors()" if the array is called "Array"?
        • 1. Re: Have a method return an array
          807600
          Well for a 2d array of ints with public access you would do this
          public int [][] methodName(parameters if any) 
          • 2. Re: Have a method return an array
            807600
            thanks. OK, now I have another, bigger problem. I am making a program that factors numbers. I decided to make "Factors" an object, and "Factors" has a size and a 2-d array within it. The problem is, when I create the "Factors" class, I don't know the dimensions of the array yet. The main mehtod will collect them later, when they are needed. Is there a way to create an array whose dimensions will be determined later in the program? Here's the code:
            class Factors{
            int size;
            int FacArray = new int [2][size];
            }

            The "size" will be specified later, but I don't know how to create the array in the class so that it can be modified in the main method.
            • 3. Re: Have a method return an array
              807600
              Build your constructors appropriately. Here is a short demonstration:
              class Factors {
              
                   private int facArray[][];
                   private int size;
              
                   public Factors(int size) {
                        this.size = size;
                        facArray = new int[2][size];          
                   }
              
                   public void setFactor(int index1, int index2, int value) {
                        facArray[index1][index2] = value;
                   }
              
                   public int getFactor(int index1, int index2) {
                        return facArray[index1][index2];
                   }
              
                   public int getSize() {
                        return this.size;
                   }
              }
              
              public class Main {
              
                   public static void main(String[] argv) {
              
                        Factors f = new Factors(10);
                        f.setFactor(0, 0, 100);
                        f.setFactor(0, 1, 200);
                        
                        System.out.println("Factor at facArray[0][0] = " + f.getFactor(0, 0));
                        System.out.println("Factor at facArray[0][1] = " + f.getFactor(0, 1));
              
                   }
              }
              • 4. Re: Have a method return an array
                807600
                Oh. I thought you had to define the dimensions of the array when it was first specified in the class. That works. Thanks.
                • 5. Re: Have a method return an array
                  807600
                  ok, next issue. When I run this program which finds the factors, its throws the nullpointerexception on line 15:



                  class Factors{
                       int FacArray[][];

                  public static void main(String args[]){
                       int a = 6;
                       int b = 6;
                       int c = 9;

                       Factors cFac = new Factors();
                       Factors aFac = new Factors();

                       cFac.FindFacs(c);
                       aFac.FindFacs(a);

                  System.out.println(cFac.FacArray[0][0]);
                  System.out.println(aFac.FacArray[0][0]);

                  }







                  int[][] FindFacs(int number){

                            int slot = 0;
                            float modulus;                              
                            int divisor, result;                         
                            int FacArray[][] = new int[2][number];
                                 
                            


                            for(divisor=1; divisor<=(number/2); divisor++) {     
                       
                                 if (number%divisor==0) {
                                                
                                      result = number/divisor;

                                 if (divisor <= result) {          
                                      FacArray[0][slot] = divisor;          
                                      FacArray[1][slot] = result;          
                                      slot++;     
                                 };                    
                                 };
                            };
                       
                            return FacArray;
                  }}
                  • 6. Re: Have a method return an array
                    807600
                    Please please learn to use code tags. If you don't format your code well, we're not going to want to read the code.

                    You have several mistakes in your code.

                    1) Your FindFacs method returns an int array, but you don't do anything with it. If you want to use it, you must do something like this:
                    int[][] myResult = cFac.FindFac(c);
                    2) Your class variable FacArray is never initialize, never filled with numbers. You assume that the FacArray in your FindFac method is the same as the class variable, but it's not. You need to use either one or the other.

                    3) When you evenutally do have a viable result, this
                            System.out.println(cFac.FacArray[0][0]);
                    is not going to output what you think it will. It takes a little more work to display the values contained in arrays.
                    • 7. Re: Have a method return an array
                      807600
                      ok, next issue. When I run this program which finds
                      the factors, its throws the nullpointerexception on
                      line 15:



                      class Factors{
                           int FacArray[][];

                      public static void main(String args[]){
                           int a = 6;
                           int b = 6;
                           int c = 9;

                           Factors cFac = new Factors();
                           Factors aFac = new Factors();

                           cFac.FindFacs(c);
                           aFac.FindFacs(a);

                      System.out.println(cFac.FacArray[0][0]);
                      System.out.println(aFac.FacArray[0][0]);

                      }







                      int[][] FindFacs(int number){

                                int slot = 0;
                                float modulus;                              
                                int divisor, result;                         
                                int FacArray[][] = new int[2][number];
                                     
                                


                                for(divisor=1; divisor<=(number/2); divisor++) {     
                           
                                     if (number%divisor==0) {
                                                    
                                          result = number/divisor;

                                     if (divisor <= result) {          
                                          FacArray[0][slot] = divisor;          
                                          FacArray[1][slot] = result;          
                                          slot++;     
                                     };                    
                                     };
                                };
                           
                                return FacArray;
                      }}[/b
                      Take another look at the implementation of the Factor class that I gave you...
                      • 8. Re: Have a method return an array
                        807600
                        but it is filled with numbers in the FindFacs() method.
                        • 9. Re: Have a method return an array
                          807600
                          Oh, and i know how to display the values in arrays; that was just there for testing.
                          • 10. Re: Have a method return an array
                            807600
                            but it is filled with numbers in the FindFacs()
                            method.
                            My point is to think "object-oriented." Java is an OO language, you know.
                            • 11. Re: Have a method return an array
                              807600
                              oh, one more question: can you call a method other than "main" without an object? (If it is in the same class, that is.)
                              • 12. Re: Have a method return an array
                                807600
                                class Foo {
                                    public static void main(String[] args) {
                                        Foo f = new Foo();
                                        f.methodA();
                                    }
                                
                                    public void methodA() {
                                        methodB();
                                    }
                                
                                    private void methodB() {
                                        System.out.println("Hello");
                                    }
                                }
                                • 13. Re: Have a method return an array
                                  807600
                                  OK,ok. Sorry my questions have been a bit choppy; i needed to get my thoughts together. So here's the situation. I want all objects of class "Factors" to have a copy of the integer factor array "FacArray". However, I don't know how to fill that copy of "FacArray" with numbers in the "FindFacs" method. So it would look like this:
                                  class Factors{
                                  int[][] FacArray;
                                  }
                                  public static void main(String args[]){
                                       int a = 6;
                                       int b = 6;
                                       int c = 9;
                                  
                                       Factors cFac = new Factors();
                                       Factors aFac = new Factors();
                                  
                                       cFac.FindFacs(c);
                                       aFac.FindFacs(a);
                                  }
                                  public int[][] FindFacs(int number){
                                  
                                            int slot = 0;
                                            float modulus;                              
                                            int divisor, result;                         
                                            int FacArray[][] = new int[2][number];
                                                 
                                            
                                  
                                  
                                            for(divisor=1; divisor<=(number/2); divisor++) {     
                                       
                                                 if (number%divisor==0) {
                                                                    
                                                      result = number/divisor;
                                  
                                                    if (divisor <= result) {          
                                                      FacArray[0][slot] = divisor;          
                                                      FacArray[1][slot] = result;          
                                                      slot++;     
                                                    };                    
                                                 };
                                            };
                                       
                                            return FacArray;
                                       }
                                  I don't understand why, when "FindFacs" returns "facArray", it does not modify the copy of "FacArray" in the objects "aFac" and "cFac".
                                  • 14. Re: Have a method return an array
                                    807600
                                    class Factors{ // class goes from here
                                    int[][] FacArray;
                                    } // to here. All other code is not in a class
                                    If you need to have all instances of the class have access to the same array, then make it static. But either you or I am confused.
                                    1 2 3 Previous Next