0
import java.util.*; ...    

ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Item");

这是我的 Java 代码。最后一行得到错误:“包 stringList 不存在”。

如何摆脱此错误并开始将项目添加到我的列表中?我只能想象我错过了一些非常简单的东西。提前致谢。

部分根据请求,以下是我的整个代码。它不完整,但我被困在上述问题上。

package gorobot_m5d25y2013;

//import java.util.List;
import java.util.ArrayList;

class Engine {  // The Engine will show the best positions on the board given a particular board setup.

    // Some project-wide constants and variables.
    final int BLACK = 1;      // Black go pieces... black goes first in the game of go, unless black has
    // a 2 stone or greater handicap.
    final int WHITE = 2;      // White go pieces... usually goes second in the game.
    final int EMPTY = 0;      // An empty space, no piece is on it.
    final int VIRTUAL = -1;   // This is a space outside of the board that can never be played on,
    // but is useful if there's ever an issue of checking something for
    // the board which would normally be out of bounds.
    final int BLACKS_TURN = 1;
    final int WHITES_TURN = 2;
    final int BOARD_SIZE = 19;              // Access with Global.BOARD_SIZE ... this is the width of the board.
    final int VIRTUAL_BOARD_BORDER = 10;    // This is the width of the empty space around each of the four sides of the board.
    int whoseTurn;                          // 1 is blacks turn, 2 is white's turn.
    int totalGames = 0;
    int blackWins = 0;
    static long groupCounter = 1; // Used in inner class group.
    int positionNumber = 1; // Used in inner class group and position.
    int[][] boardValues = new int[BOARD_SIZE][BOARD_SIZE];  // boardValues is the win probability for each position on the board.

    Engine() {
        Board firstMove = new Board();
    }

    int oppositeWhoseTurn() {
        int opposite = 0;
        if (whoseTurn == BLACKS_TURN) {
            opposite = WHITES_TURN;
        }
        if (whoseTurn == WHITES_TURN) {
            opposite = BLACKS_TURN;
        }
        if (opposite == 0) {
            System.out.println("An error with the opposite variable has occured.");
        }
        return opposite;
    }

    int regPos(int boardPosition) { // This Regulates Position-Ex. takes any of the normal board positions, for example,
        // 1-19, and sets it to the virtual, larger board. So a Position 1 move will be position 9 with a 10 space buffer.
        int realBoardPosition = (boardPosition + (VIRTUAL_BOARD_BORDER - 1));
        return realBoardPosition;
    }

    class Board { // The board is composed of positions and groups.  At first it is 2 groups, the real and the virtual

        int virtualPlusRegularBoardSize = (BOARD_SIZE + (VIRTUAL_BOARD_BORDER * 2));
        Position[][] point = new Position[virtualPlusRegularBoardSize][virtualPlusRegularBoardSize];
        Group food = new Group();

        Board() {
            for (int horizontal = 0; horizontal < virtualPlusRegularBoardSize; horizontal++) {
                for (int vertical = 0; vertical < virtualPlusRegularBoardSize; vertical++) {
                    point[horizontal][vertical] = new Position(horizontal, vertical);
                }
            }
            for (int horizontal = 1; horizontal <= BOARD_SIZE; horizontal++) {
                for (int vertical = 1; vertical <= BOARD_SIZE; vertical++) {
                    point[regPos(horizontal)][regPos(vertical)].setType(EMPTY);
                }
            }
        }
    }

    class Group { // Each Group is composed of positions

        long groupNumber;
        int sizeOfGroup = 1;
        Integer[] ints = new Integer[1];
        ArrayList<String> stringList = new ArrayList<String>();
        stringList.add ("Item");


        //List<Position> positionList = new ArrayList<>();

        //positionList.size ();

        //positionList.add ();
        //positionList.remove("F");

        Group() {
            groupNumber = groupCounter;
            groupCounter++;
        }

        void setGroupNumber(int newGroupNumber) {
            sizeOfGroup = newGroupNumber;
        }

        long getGroupNumber() {
            return groupNumber;
        }

        void setSizeOfGroup(int newGroupSize) {
            sizeOfGroup = newGroupSize;
        }

        int getSizeOfGroup() {
            return sizeOfGroup;
        }

        void addPositionToGroup(Position point) {
        }
    }

    class Position {    // Each position is either Empty, Black, White, or Virtual.

        int pieceType;  // VIRTUAL means it's not even a board position, at first all spaces are like this...
        // but eventually, the positions on the actual board will all be empty - 0, and then
        // will start filling with black - 1 and white - 2 pieces.
        boolean legalMove; // This tells us whether this space is a legal move.
        int numberOfLiberties = 2;
        int virtualHorizontal;
        int virtualVertical;

        Position() {
            pieceType = VIRTUAL;
            legalMove = false;
        }

        Position(int horizontal, int vertical) {
            pieceType = VIRTUAL;
            legalMove = true;
            virtualHorizontal = horizontal;
            virtualVertical = vertical;
        }

        void setType(int setType) {
            pieceType = setType;
            ////// Add in here the 4 surrounding sides and make those these variables accessible from the piece class.
            if (setType == BLACK) {
            }
            if (setType == WHITE) {
            }
        }

        int getType() {
            return pieceType;
        }
    }
}

/**
 *
 * @author Eric Martin
 */
public class GoRobot_m5d25y2013 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Engine firstEngine = new Engine();
    }
}

这些是我的错误:

run:
Exception in thread "main" java.lang.ClassFormatError: Method "<error>" in class gorobot_m5d25y2013/Engine$Group has illegal signature "(Ljava/lang/Object;)LstringList/add;"
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at gorobot_m5d25y2013.Engine$Board.<init>(GoRobot_m5d25y2013.java:73)
    at gorobot_m5d25y2013.Engine.<init>(GoRobot_m5d25y2013.java:46)
    at gorobot_m5d25y2013.GoRobot_m5d25y2013.main(GoRobot_m5d25y2013.java:181)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
4

3 回答 3

2

从您的编辑来看,问题在于您正在调用stringList.add("Item");类定义。类字段中方法的执行应该在方法内完成。在您的情况下,看起来这一行应该在您的类构造函数中:

class Group { // Each Group is composed of positions
    //fields definitions (and probably initialization of fields)...
    //field declaration
    ArrayList<String> stringList = new ArrayList<String>();
    //this code can't be here
    //stringList.add("Item");

    Group() {
        //move it here
        stringList.add("Item");
        //...
    }

    //rest of code...
}
于 2013-05-30T03:08:53.263 回答
1

这段代码没有产生任何错误,我认为你没有把它放在课堂上。你可以试试这个。

import java.util.*;

public class One {

    public static void main(String args[]) {
        ArrayList<String> stringList=new ArrayList<String>();
        stringList.add("Europe");
        System.out.println(stringList);
    }
}
于 2013-05-30T02:56:41.320 回答
0

您应该创建一个组对象。然后使用该对象访问列表。

在主要方法中尝试

 Group g = new Group();
 g.stringList.add("item");
于 2013-05-30T03:09:20.217 回答