14 Replies Latest reply: Apr 30, 2008 11:04 AM by 800435 RSS

    Array Problems

    807601
      So, I'm supposed to make a program with two classes. One with an array constructor and several methods, and another that creates an instance fro the other class, initializes it with 10 specific numbers, then runs several methods. I think all my methods look right, but I'm having trouble with what should be the easy part. Please help!


      public class Data
      {
         
      public Data(int[] dataSet)
         {
            dataSet = new int[ARRAY_SIZE];
            dataSet[0] = 28;
            dataSet[1] = -8;
            dataSet[2] = -15;
            dataSet[3] = 12;
            dataSet[4] = 5;
            dataSet[5] = -10;
            dataSet[6] = 21;
            dataSet[7] = 18;
            dataSet[8] = -23;
            dataSet[9] = -15;
         }
      
      public static void printElements(int[] myArray)
      {
         for (int e : myArray)
            System.out.println(e);
      }
      
      public int[] reverse(int[] oldArray)
      {
         int[] newArray = new int[oldArray.length];
         for (int i = 0; i < oldArray.length; i++ )
            {
            newArray[oldArray.length - i] = oldArray; 
      }
      return newArray;
      }

      public int countNeg(int[] anArray)
      {
      int negTotal = 0;
      for (int e : anArray)
      if (e < 0)
      negTotal++;
      return negTotal;
      }

      public boolean hasRepeated(int[] anArray2)
      {
      for (int j = 0; j<anArray2.length; j++)
      {
      for (int k = 0; k < j; k++)
      if (anArray2[k] == anArray2[j] && j != k);
      }
      return true;

      }

      public int[] dataArray;
      public int[] dataSet;
      private static final int ARRAY_SIZE = 10;
      }

      public class DataTester
      {
      public static void main(String[] args)
      {
      Data data = new Data();
      Data.printElements(data);
      }

      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        • 1. Re: Array Problems
          807601
          first off change your constructor to
          public Data()
             {
                 ...
             }
          also you are passing a Data object to printElements() which takes an int array as arguments
          • 2. Re: Array Problems
            807601
            I guess I don't understand why I even need a constructor in the first place. Why not just create the array in the main method and then use the various methods on it?
            • 3. Re: Array Problems
              807601
              yes you don't necessarily need one. you can create the array in main() and then use a setArray(int[] myArray) to set the corresponding field in the object
              • 4. Re: Array Problems
                800435
                public class Data
                {
                public int[] dataArray;//you don't need this
                public int[] dataSet;//you don't need this since all your methods receive an array as argument
                private static final int ARRAY_SIZE = 10;
                public Data()
                   {
                   
                   }
                 
                public static void printElements(int[] myArray)
                {
                   for (int e : myArray)
                      System.out.println(e);
                }
                 
                public int[] reverse(int[] oldArray)
                {
                   int[] newArray = new int[oldArray.length];
                   for (int i = 0; i < oldArray.length; i++ )
                      {
                      newArray[oldArray.length - i] = oldArray; 
                }
                return newArray;
                }

                public int countNeg(int[] anArray)
                {
                int negTotal = 0;
                for (int e : anArray)
                if (e < 0)
                negTotal++;
                return negTotal;
                }

                public boolean hasRepeated(int[] anArray2)
                {
                for (int j = 0; j<anArray2.length; j++)
                {
                for (int k = 0; k < j; k++)
                if (anArray2[k] == anArray2[j] && j != k); //are you missing something here? If this is true what happens?
                }
                return true;

                }

                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                • 5. Re: Array Problems
                  800435
                  And in your main method
                  public class DataTester
                  {
                     public static void main(String[] args)
                     {
                        dataSet = new int[10];
                        dataSet[0] = 28;
                        dataSet[1] = -8;
                        dataSet[2] = -15;
                        dataSet[3] = 12;
                        dataSet[4] = 5;
                        dataSet[5] = -10;
                        dataSet[6] = 21;
                        dataSet[7] = 18;
                        dataSet[8] = -23;
                        dataSet[9] = -15;
                  
                        Data data = new Data();
                        Data.printElements(dataSet);
                     }
                   
                  }
                  • 6. Re: Array Problems
                    807601
                    That gives me the same problem I had earlier:

                    The printElements method isn't working. Says the method isn't applicable to the type data.
                    • 7. Re: Array Problems
                      807601
                      manuel.leiria wrote:
                      Data data = new Data();
                      Data.printElements(dataSet);
                      That first line is pointless, since you're calling the method statically directly on the class, so you never use the data object.

                      To the OP: what exact error message are you getting. Don't paraphrase. Also, post the most up to date version of your code.
                      • 8. Re: Array Problems
                        807601
                        Error Message:
                        The method printElements(int[]) in the type data is not applicable for the arguments (Data)
                        public class DataTester
                        {
                           public static void main(String[] args)
                           {
                              int[] dataSet = new int[10];
                              dataSet[0] = 28;
                              dataSet[1] = -8;
                              dataSet[2] = -15;
                              dataSet[3] = 12;
                              dataSet[4] = 5;
                              dataSet[5] = -10;
                              dataSet[6] = 21;
                              dataSet[7] = 18;
                              dataSet[8] = -23;
                              dataSet[9] = -15;
                         
                              Data data = new Data();
                              Data.printElements(data);
                           }
                         
                        }
                        public class Data
                        {
                        public Data()
                           {
                           
                           }
                         
                        public static void printElements(int[] myArray)
                        {
                           for (int e : myArray)
                              System.out.println(e);
                        }
                         
                        public int[] reverse(int[] oldArray)
                        {
                           int[] newArray = new int[oldArray.length];
                           for (int i = 0; i < oldArray.length; i++ )
                              {
                              newArray[oldArray.length - i] = oldArray; 
                        }
                        return newArray;
                        }

                        public int countNeg(int[] anArray)
                        {
                        int negTotal = 0;
                        for (int e : anArray)
                        if (e < 0)
                        negTotal++;
                        return negTotal;
                        }

                        public boolean hasRepeated(int[] anArray2)
                        {
                        for (int j = 0; j<anArray2.length; j++)
                        {
                        for (int k = 0; k < j; k++)
                        if (anArray2[k] == anArray2[j] && j != k); //are you missing something here? If this is true what happens?
                        }
                        return true;

                        }

                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                        • 9. Re: Array Problems
                          800435
                          newark wrote:
                          manuel.leiria wrote:
                          Data data = new Data();
                          Data.printElements(dataSet);
                          That first line is pointless, since you're calling the method statically directly on the class, so you never use the data object.

                          To the OP: what exact error message are you getting. Don't paraphrase. Also, post the most up to date version of your code.
                          Yes. The first line is just prepared to use in the other methods
                          • 10. Re: Array Problems
                            800435
                            ZachW wrote:
                            Error Message:
                            The method printElements(int[]) in the type data is not applicable for the arguments (Data)
                            public class DataTester
                            {
                            public static void main(String[] args)
                            {
                            int[] dataSet = new int[10];
                            dataSet[0] = 28;
                            dataSet[1] = -8;
                            dataSet[2] = -15;
                            dataSet[3] = 12;
                            dataSet[4] = 5;
                            dataSet[5] = -10;
                            dataSet[6] = 21;
                            dataSet[7] = 18;
                            dataSet[8] = -23;
                            dataSet[9] = -15;
                            
                            Data data = new Data();
                            Data.printElements(data);
                            }
                            
                            }
                            public class Data
                            {
                            public Data()
                            {
                            
                            }
                            
                            public static void printElements(int[] myArray)
                            {
                            for (int e : myArray)
                            System.out.println(e);
                            }
                            
                            public int[] reverse(int[] oldArray)
                            {
                            int[] newArray = new int[oldArray.length];
                            for (int i = 0; i < oldArray.length; i++ )
                            {
                            newArray[oldArray.length - i] = oldArray; 
                            }
                            return newArray;
                            }

                            public int countNeg(int[] anArray)
                            {
                            int negTotal = 0;
                            for (int e : anArray)
                            if (e < 0)
                            negTotal++;
                            return negTotal;
                            }

                            public boolean hasRepeated(int[] anArray2)
                            {
                            for (int j = 0; j<anArray2.length; j++)
                            {
                            for (int k = 0; k < j; k++)
                            if (anArray2[k] == anArray2[j] && j != k); //are you missing something here? If this is true what happens?
                            }
                            return true;

                            }

                            }

                            Of course. The printElements method is expecting an array of int. Try this:
                            public static void main(String[] args)
                                {
                                   int[] dataSet = new int[10];
                                   dataSet[0] = 28;
                                   dataSet[1] = -8;
                                   dataSet[2] = -15;
                                   dataSet[3] = 12;
                                   dataSet[4] = 5;
                                   dataSet[5] = -10;
                                   dataSet[6] = 21;
                                   dataSet[7] = 18;
                                   dataSet[8] = -23;
                                   dataSet[9] = -15;
                              
                                   Data data = new Data();
                                   Data.printElements(dataSet);
                               }
                            Edited by: manuel.leiria on Apr 30, 2008 5:07 PM
                            • 11. Re: Array Problems
                              807601
                              ZachW wrote:
                              Error Message:
                              The method printElements(int[]) in the type data is not applicable for the arguments (Data)
                              public class DataTester
                              {
                              public static void main(String[] args)
                              {
                              int[] dataSet = new int[10];
                              dataSet[0] = 28;
                              dataSet[1] = -8;
                              dataSet[2] = -15;
                              dataSet[3] = 12;
                              dataSet[4] = 5;
                              dataSet[5] = -10;
                              dataSet[6] = 21;
                              dataSet[7] = 18;
                              dataSet[8] = -23;
                              dataSet[9] = -15;
                              
                              Data data = new Data();
                              Data.printElements(data);
                              }
                              
                              }
                              the printElements() method takes an integer array
                              public static void printElements(int[] myArray)
                              you're passing it a Data object
                              Data data = new Data();
                                     Data.printElements(data);
                              Pass it dataSet, not data
                              • 12. Re: Array Problems
                                807601
                                Newark - you are a living deity, and the idol of all my affections for now until eternity.
                                • 13. Re: Array Problems
                                  807601
                                  Well, to be fair, manuel.leiria said the same thing right above me :)
                                  But thanks!
                                  • 14. Re: Array Problems
                                    800435
                                    newark wrote:
                                    Well, to be fair, manuel.leiria said the same thing right above me :)
                                    But thanks!
                                    no problemo ;)