java - 如何使数组成为最终数组?

标签 java final gridworld

我似乎无法弄清楚这个“世界必须被声明为最终”的错误。有什么建议吗?我正在制作一个网格世界项目(配对游戏)。该段

  temparray.add(world.getLocation().getColor() );

要求它是最终的...但是添加最终的 world.get 等表示非法的表达式开始。

import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import info.gridworld.world.*;



import java.awt.Color;
import java.util.ArrayList;

public class cardGameDriver extends ActorWorld
   {
   public static void main( String[] args )
  {
  World world = new World();
  world.show();

  world.add( new card1());
  world.add( new card1()); 
  world.add( new card2()); 
  world.add( new card2());
  world.add( new card3());
  world.add( new card3());
  world.add( new card4());
  world.add( new card4());
  world.add( new card5());
  world.add( new card5());
  world.add( new card6());
  world.add( new card6());
  world.add( new card7());
  world.add( new card7());
  world.add( new card8());
  world.add( new card8());
  world.add( new card9());
  world.add( new card9());
  world.add( new card10());
  world.add( new card10());
  world.add( new card11());
  world.add( new card11());
  world.add( new card12());
  world.add( new card12());
  world.add( new card13());
  world.add( new card13());
  world.add( new card14());
  world.add( new card14());
  world.add( new card15());
  world.add( new card15());
  world.add( new card16());
  world.add( new card16());
  world.add( new card17());
  world.add( new card17());
  world.add( new card18());
  world.add( new card18());
  System.out.println( "Time yourself to see how fast you can make all 18 matches." );


  ArrayList<cards> temparray = new ArrayList<cards>();


  java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager() 
  .addKeyEventDispatcher(new java.awt.KeyEventDispatcher() 
  {
  public boolean dispatchKeyEvent(java.awt.event.KeyEvent event) 
     { 
     String key = javax.swing.KeyStroke.getKeyStrokeForEvent(event).toString(); 
     int x = 0;
     if (key.equals("pressed ENTER")) 
        {
        temparray.add(world.getLocation().getColor() );
        x++;
        if( x == 2 )
           {
           if( temparray.get(0) == temparray.get(1) )
              {
              System.out.println( "User has made a match!" );
              }
           else
              {
              System.out.println( "Try again!" );
              }
           x = 0;
           }
        }
     return true; 
       } 
  }); 

  }

}

最佳答案

由于您尝试在方法中的局部类内使用局部变量,因此必须将该变量声明为 final。因此,只需将 final 修饰符添加到变量声明中即可:

final World world = new World();

main 方法本地的其他变量类似,但不适用于本地类,例如临时数组

关于java - 如何使数组成为最终数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184658/

相关文章:

java - 如何将 Hibernate 列表转换为字符串?

java - 替换 HTTP 请求字符串中的 Host

java - 无法引用封闭范围中定义的非最终局部变量显示

java - 使用 Gridworld 的跳棋游戏 (Java)

java - GridWorld 将花添加到随机位置

javascript - 编程基于坐标的游戏,例如点或跳棋

java - 通过java中的socket编程检查远程服务器是否处于监听模式

java - 如何将多维 JSON 数组转换为多维 Java 数组

java - 被困在访问非最终变量上?

java - Eclipse - 默认情况下使重写方法和类最终