在Java中获取错误“包X不存在”

时间:2013-05-30 02:45:52

标签: java arraylist

import java.util.*; ...    

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

这是我的Java代码。最后一行得到错误:“package 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();
    }
}

这些是我的错误:

运行: 线程“main”java.lang.ClassFormatError中的异常:类gorobot_m5d25y2013 / Engine $中的方法“组具有非法签名”(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)     在java.net.URLClassLoader.access $ 100(URLClassLoader.java:71)     在java.net.URLClassLoader $ 1.run(URLClassLoader.java:361)     在java.net.URLClassLoader $ 1.run(URLClassLoader.java:355)     at java.security.AccessController.doPrivileged(Native Method)     在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)     在gorobot_m5d25y2013.Engine $ Board。(GoRobot_m5d25y2013.java:73)     在gorobot_m5d25y2013.Engine。(GoRobot_m5d25y2013.java:46)     在gorobot_m5d25y2013.GoRobot_m5d25y2013.main(GoRobot_m5d25y2013.java:181) Java结果:1 建立成功(总时间:2秒)

3 个答案:

答案 0 :(得分:1)

从您的编辑中,问题是您在类定义中调用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...
}

答案 1 :(得分:0)

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

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);

}

}

答案 2 :(得分:0)

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

在主要方法中尝试

 Group g = new Group();
 g.stringList.add("item");