数组列表不会使用我创建的方法

时间:2014-01-13 03:53:04

标签: java methods arraylist

我应该创建方法a-j,然后在数组列表中使用它们。我已经创建了所有这些方法,它们应该正常工作,但每当我尝试使用它们时,它都会给我一个 找不到标志。我确定这是显而易见的,但我只是没有看到它。

非常感谢任何帮助,谢谢

以下是代码:

//Ian G
//AList-Asg2: ArrayLists Bk Program

import java.util.ArrayList;

class Bell
{
   private int studentId;

   public Bell( int id )
   {studentId = id;}

   public int id()
   {return studentId;}

   public void setId(int newId)
   {studentId = newId;}

   public int compareTo( Bell otherBell )
   {return this.studentId - otherBell.studentId;}

   public String toString()
   { return ""+studentId;}
}


class BellTester_Granger
{

   public ArrayList<Bell> rayList;

   public static void main(String[] args)
   {

      ArrayList<Bell> rayList = new ArrayList<Bell>();
      Bell student1 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student2 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student3 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student4 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student5 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student6 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student7 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student8 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student9 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student10 = new Bell((int)(Math.random() * 1000) + 216000);
      rayList.add(student1);
      rayList.add(student2);
      rayList.add(student3);
      rayList.add(student4);
      rayList.add(student5);
      rayList.add(student6);
      rayList.add(student7);
      rayList.add(student8);
      rayList.add(student9);
      rayList.add(student10);


      /**
      Here i'm trying to use one of the method's that i created, but it's giving me the error
      */

      rayList.swapFL();   

   }

   //a 
   public void swapFL()
   {   
      int f= (rayList.get(0)).id();
      int l= (rayList.get(rayList.size()-1)).id();
      (rayList.get(0)).setId(l);
      (rayList.get(rayList.size()-1)).setId(l);
   }

   //b
   public void shiftR()
   {   
      rayList.add(0, rayList.get(rayList.size()-1));
      rayList.remove(rayList.size()-1);
   }

   //c
   public void replaceEven()
   {   
      for( Bell b: rayList)
      {
         if (b.id()%2 == 0)
         {
            b.setId(216222);
         }
      }
   }

   //d
   public void setToLarger()
   {   
      for(int c=1; c<rayList.size()-1; c++)
      {
         if((rayList.get(c-1)).id() > rayList.get(c+1).id())
         {
            (rayList.get(c)).setId((rayList.get(c-1)).id());
         }
         else
         {
            (rayList.get(c)).setId((rayList.get(c+1)).id());
         }
      }
   }

   //e
   public void removeMiddle()
   {
      rayList.remove(5);
      rayList.remove(6);
   }

   //f
   public void evenToFront()
   {   
      for(int c=0; c<rayList.size(); c++)
      {
         if (rayList.get(c).id()%2 == 0)
         {
            rayList.set(0,rayList.get(c));
            rayList.remove(c);
         }
      }
   }

   //g
   public void secondLargest()
   {   
      int largest = 0;
      int secondLargest = -1;
      for( Bell b: rayList)
      {
         if (b.id() > largest)
         {
            largest = b.id();
         }
      }

      for( Bell b: rayList)
      {
         if (b.id() > secondLargest && b.id() < largest)
         {
            secondLargest = b.id();
         }
      }            
   }

   //h
   public boolean increasingOrder()
   {   
      for(int c=1; c<rayList.size()-1; c++)
      {
         if (!(rayList.get(c).id()>rayList.get(c-1).id() && rayList.get(c).id()>rayList.get(c+1).id()))
         {
            return false;
         }
      }
      return true;
   }

   //i
   public boolean adjacentDupiclates()
   {   
      for(int c=0; c<rayList.size()-1; c++)
      {
         if (rayList.get(c).id()==rayList.get(c+1).id())
         {
            return true;
         }
      }
      return false;
   }

   //j
   public boolean duplicates()
   {
      int c=0;
      while(c< rayList.size())
      {
         for( Bell b: rayList)
         {
            if (b.id() == rayList.get(c).id())
            {
               return true;
            }
         }
         c++;
      }
      return false;
   }
}

4 个答案:

答案 0 :(得分:1)

swapFLBellTester_Granger的实例方法,而不是内置类java.util.ArrayList

BellTester_Granger granger = new BellTester_Granger();
// rayList.swapFL();  oh oh
granger.swapFL();

附注:在命名类时遵循Java命名约定,例如BellTesterGranger

答案 1 :(得分:0)

我浏览代码时看到的两个问题。

首先是您使用主要方法中定义的rayList隐藏了类别rayList。只需删除重新定义

即可
 public static void main(String[] args)
 {

      ArrayList<Bell> rayList = new ArrayList<Bell>();  
      // rest of your main method

  }

只是初学而不是重新定义:

    rayList = new ArrayList<Bell>(); 

第二个问题是您无需使用swapFl调用rayList方法,因为ArrayList类没有任何此类方法。它是具有此方法的BellTester_Granger类,因此请使用其中提到的isntance调用它:

BellTester_Granger grangerInstance = new BellTester_Granger();
grangerInstance .swapFL();

答案 2 :(得分:0)

BellTest_Granger类中定义方法不会将它们添加到ArrayTest个对象中。您应该创建一个继承自ArrayList的类,并将方法添加到那个类,或者将方法定义为static并将ArrayList作为参数传递给它们:

public static void swapFL(ArrayList<Bell> list)
{   
   int f= (list.get(0)).id();
   int l= (list.get(list.size()-1)).id();
   (list.get(0)).setId(l);
   (list.get(rayList.size()-1)).setId(l);
}

然后你可以使用

swapFL(rayList);

但是,在任何一种方法中,通常使用泛型并且没有硬编码的功能只能用于ArrayList<Bell>个对象:

public static <T> void shiftR(ArrayList<T> list)
{   
   list.add(0, list.get(list.size()-1));
   list.remove(list.size()-1);
} 

再次,你可以用:

来调用它
shiftR(rayList);

答案 3 :(得分:0)

您的代码中存在多个问题,函数swapFL不正确,我更新您的代码,并使用SwapFL的简单案例进行测试,它有效,希望它会有所帮助,

import java.util.ArrayList;

class Bell
{
   private int studentId;

   public Bell( int id )
   {studentId = id;}

   public int id()
   {return studentId;}

   public void setId(int newId)
   {studentId = newId;}

   public int compareTo( Bell otherBell )
   {return this.studentId - otherBell.studentId;}

   public String toString()
   { return ""+studentId;}
}


class BellTester_Granger 
{

   public static ArrayList<Bell> rayList;

   public static void main(String[] args)
   {

      rayList = new ArrayList<Bell>();
      Bell student1 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student2 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student3 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student4 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student5 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student6 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student7 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student8 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student9 = new Bell((int)(Math.random() * 1000) + 216000);
      Bell student10 = new Bell((int)(Math.random() * 1000) + 216000);
      rayList.add(student1);
      rayList.add(student2);
      rayList.add(student3);
      rayList.add(student4);
      rayList.add(student5);
      rayList.add(student6);
      rayList.add(student7);
      rayList.add(student8);
      rayList.add(student9);
      rayList.add(student10);


      /**
      Here i'm trying to use one of the method's that i created, but it's giving me the error
      */
      for(int i =0; i< rayList.size(); i++)
      {
          System.out.println(rayList.get(i));
      }
      swapFL();   
      System.out.println("after swapFL");
      for(int i =0; i< rayList.size(); i++)
      {
          System.out.println(rayList.get(i));
      }
   }

   //a 
   public static void swapFL()
   {   
      int f= (rayList.get(0)).id();
      int l= (rayList.get(rayList.size()-1)).id();
      (rayList.get(0)).setId(l);
      (rayList.get(rayList.size()-1)).setId(f);
   }

   //b
   public static void shiftR()
   {   
      rayList.add(0, rayList.get(rayList.size()-1));
      rayList.remove(rayList.size()-1);
   }

   //c
   public static void replaceEven()
   {   
      for( Bell b: rayList)
      {
         if (b.id()%2 == 0)
         {
            b.setId(216222);
         }
      }
   }

   //d
   public static void setToLarger()
   {   
      for(int c=1; c<rayList.size()-1; c++)
      {
         if((rayList.get(c-1)).id() > rayList.get(c+1).id())
         {
            (rayList.get(c)).setId((rayList.get(c-1)).id());
         }
         else
         {
            (rayList.get(c)).setId((rayList.get(c+1)).id());
         }
      }
   }

   //e
   public static void removeMiddle()
   {
      rayList.remove(5);
      rayList.remove(6);
   }

   //f
   public static void evenToFront()
   {   
      for(int c=0; c<rayList.size(); c++)
      {
         if (rayList.get(c).id()%2 == 0)
         {
            rayList.set(0,rayList.get(c));
            rayList.remove(c);
         }
      }
   }

   //g
   public static void secondLargest()
   {   
      int largest = 0;
      int secondLargest = -1;
      for( Bell b: rayList)
      {
         if (b.id() > largest)
         {
            largest = b.id();
         }
      }

      for( Bell b: rayList)
      {
         if (b.id() > secondLargest && b.id() < largest)
         {
            secondLargest = b.id();
         }
      }            
   }

   //h
   public static boolean increasingOrder()
   {   
      for(int c=1; c<rayList.size()-1; c++)
      {
         if (!(rayList.get(c).id()>rayList.get(c-1).id() && rayList.get(c).id()>rayList.get(c+1).id()))
         {
            return false;
         }
      }
      return true;
   }

   //i
   public static boolean adjacentDupiclates()
   {   
      for(int c=0; c<rayList.size()-1; c++)
      {
         if (rayList.get(c).id()==rayList.get(c+1).id())
         {
            return true;
         }
      }
      return false;
   }

   //j
   public static boolean duplicates()
   {
      int c=0;
      while(c< rayList.size())
      {
         for( Bell b: rayList)
         {
            if (b.id() == rayList.get(c).id())
            {
               return true;
            }
         }
         c++;
      }
      return false;
   }
}