Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组列表赢了';t使用方法I';我创造了_Java_Methods_Arraylist - Fatal编程技术网

Java 数组列表赢了';t使用方法I';我创造了

Java 数组列表赢了';t使用方法I';我创造了,java,methods,arraylist,Java,Methods,Arraylist,我应该创建方法a-j,然后在数组列表中使用它们。我已经创建了所有这些方法,它们应该能够正常工作,但每当我尝试使用它们时,它都会给我一个机会 找不到符号。我相信这是显而易见的,但我只是没看到而已 任何帮助都将不胜感激,谢谢 代码如下: //Ian G //AList-Asg2: ArrayLists Bk Program import java.util.ArrayList; class Bell { private int studentId; public Bell( int

我应该创建方法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;
   }
}
//伊恩·G
//AList-Asg2:ArrayLists Bk程序
导入java.util.ArrayList;
上课铃
{
私立国际学生;
公共铃(国际识别号)
{studentId=id;}
公共int id()
{return studentId;}
公共void setId(int newId)
{studentId=newId;}
公共内部比较(贝尔其他贝尔)
{返回this.studentId-otherBell.studentId;}
公共字符串toString()
{return”“+studentId;}
}
贝尔·格兰杰类
{
公共阵列列表;
公共静态void main(字符串[]args)
{
ArrayList光线列表=新建ArrayList();
贝尔学生1=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生2=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生3=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生4=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生5=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生6=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生7=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生8=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生9=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生10=新贝尔((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(学生7);
rayList.add(student8);
rayList.add(student9);
rayList.add(student10);
/**
这里我尝试使用我创建的一个方法,但它给了我错误
*/
rayList.swapFL();
}
//a
公共空间交换
{   
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
公共空间转移
{   
添加(0,rayList.get(rayList.size()-1));
rayList.remove(rayList.size()-1);
}
//c
公营机构
{   
用于(贝尔b:光线列表)
{
如果(b.id()%2==0)
{
b、 setId(216222);
}
}
}
//d
public void setToLarger()
{   
for(int c=1;c rayList.get(c+1.id())
{
(rayList.get(c)).setId((rayList.get(c-1)).id());
}
其他的
{
(rayList.get(c)).setId((rayList.get(c+1)).id());
}
}
}
//e
public void removeMidel()
{
rayList.remove(5);
射线列表。移除(6);
}
//f
公开无效事件
{   
对于(int c=0;c最大)
{
最大=b.id();
}
}
用于(贝尔b:光线列表)
{
如果(b.id()>第二大和&b.id()<最大)
{
第二大=b.id();
}
}            
}
//h
公共布尔递增顺序()
{   
对于(int c=1;crayList.get(c-1.id()&&rayList.get(c.id()>rayList.get(c+1.id()))
{
返回false;
}
}
返回true;
}
//我
公共布尔邻接重复()
{   

for(int c=0;c
swapFL
BellTester\u Granger
的一个实例方法,而不是内置类
java.util.ArrayList

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

旁注:命名类时遵循Java命名约定,例如
BellTesterRanger

我在浏览代码时看到了两个问题

首先是使用在主方法中定义的
rayList
来隐藏类杠杆
rayList
。只需删除重新定义即可

 public static void main(String[] args)
 {

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

  }

BellTest\u 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);
}
但是,在这两种方法中,通常最好使用泛型,而不要使用硬编码的功能,以仅处理
ArrayList
对象:

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

你的代码中有不止一个问题,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;
   }
}
import java.util.ArrayList;
上课铃
{
私立国际学生;
公共铃(国际识别号)
{studentId=id;}
公共int id()
{return studentId;}
公共void setId(int newId)
{studentId=newId;}
公共内部比较(贝尔其他贝尔)
{返回this.studentId-otherBell.studentId;}
公共字符串toString()
{return”“+studentId;}
}
贝尔·格兰杰类
{
公共静态数组列表;
公共静态void main(字符串[]args)
{
光线列表=新的ArrayList();
贝尔学生1=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生2=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生3=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生4=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生5=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生6=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生7=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生8=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生9=新贝尔((int)(Math.random()*1000)+216000);
贝尔学生10=新贝尔((int)(Math.random()*1000)+216000);
swapFL(rayList);
public static <T> void shiftR(ArrayList<T> list)
{   
   list.add(0, list.get(list.size()-1));
   list.remove(list.size()-1);
} 
shiftR(rayList);
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;
   }
}