So I'm trying to run my pacman project as a jar(also tried runnable) and I just get the error message you see in the title. It runs perfectly fine in eclipse/netbeans, but whilst cleaning/building i see the warnings:
Note: C:\Users\Lucas\Documents\Eclipse\PackMan\src\game\packman\GameData.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
The main class is correct and assigned. Does anybody know what I'm doing wrong?
Here is my GameData class
public class GameData {
int mazeNo;
CopyOnWriteArrayList<Position> pills;
CopyOnWriteArrayList<Position> powerPills;
public MoverInfo packman;
public GhostInfo[] ghostInfos = new GhostInfo[4];
public int score;
Maze[] mazes;
boolean dead = false;
boolean win = false;
public GameData() {
mazes = new Maze[4];
// load mazes information
for (int m=0; m<4; m++) {
mazes[m] = new Maze(m);
}
setMaze(mazeNo);
}
private void setMaze(int m) {
packman = new MoverInfo(mazes[m].packmanPos);
for (int g=0; g<4; g++) {
ghostInfos[g] = new GhostInfo(mazes[m].ghostPos);
}
pills = new CopyOnWriteArrayList((List<Position>)(mazes[m].pills.clone()));
powerPills = new CopyOnWriteArrayList((List<Position>)(mazes[m].powerPills.clone()));
}
public void movePackMan(int reqDir) {
if (move(reqDir, packman)) {
packman.curDir = reqDir;
} else {
move(packman.curDir, packman);
}
}
private int wrap(int value, int incre, int max) {
return (value+max+incre)%max;
}
private boolean move(int reqDir, MoverInfo info) {
// current position of packman is (row, column)
int row = info.pos.row;
int column = info.pos.column;
int rows = mazes[mazeNo].rows;
int columns = mazes[mazeNo].columns;
int nrow = wrap(row, MoverInfo.DROW[reqDir], rows);
int ncol = wrap(column, MoverInfo.DCOL[reqDir], columns);
if (mazes[mazeNo].charAt(nrow, ncol) != '0') {
info.pos.row = nrow;
info.pos.column = ncol;
return true;
}
return false;
}
public void update() {
if (pills.contains(packman.pos)) {
pills.remove(packman.pos);
score += 5;
} else if (powerPills.contains(packman.pos)) {
powerPills.remove(packman.pos);
score += 50;
for (GhostInfo g:ghostInfos) {
g.edibleCountDown = 500;
}
}
for (GhostInfo g:ghostInfos) {
if (g.edibleCountDown > 0) {
if (touching(g.pos, packman.pos)) {
// eat the ghost and reset
score += 100;
g.curDir = g.reqDir = MoverInfo.LEFT;
g.pos.row = mazes[mazeNo].ghostPos.row;
g.pos.column = mazes[mazeNo].ghostPos.column;
g.edibleCountDown = 0;
}
g.edibleCountDown--;
} else {
if (touching(g.pos, packman.pos)) {
dead = true;
}
}
}
// level is cleared
if (pills.isEmpty() && powerPills.isEmpty()) {
mazeNo++;
if (mazeNo < 4) {
setMaze(mazeNo);
} else if (mazeNo == 5) {
win = true;
} else {
// game over
dead = true;
}
}
}
private boolean touching(Position a, Position b) {
return Math.abs(a.row-b.row) + Math.abs(a.column-b.column) < 3;
}
public void moveGhosts(int[] reqDirs) {
for (int i=0; i<4; i++) {
GhostInfo info = ghostInfos[i];
info.reqDir = reqDirs[i];
if (move(info.reqDir, info)) {
info.curDir = info.reqDir;
} else {
move(info.curDir, info);
}
}
}
public int getWidth() {
return mazes[mazeNo].width;
}
public int getHeight() {
return mazes[mazeNo].height;
}
public List<Integer> getPossibleDirs(Position pos) {
List<Integer> list = new ArrayList<>();
for (int d=0; d<4;d++) {
Position npos = getNextPositionInDir(pos, d);
if (mazes[mazeNo].charAt(npos.row, npos.column) != '0') {
list.add(d);
}
}
return list;
}
private Position getNextPositionInDir(Position pos, int d) {
int nrow = wrap(pos.row, MoverInfo.DROW[d], mazes[mazeNo].rows);
int ncol = wrap(pos.column, MoverInfo.DCOL[d], mazes[mazeNo].columns);
return new Position(nrow, ncol);
}
}
Related
I am not an expert on java and have run into an issue on my Snake Game. I have created a class called GameManager:
public class GameManager {
private GameObject board[][];
private int xR;
private int yR;
public Snake snk;
private Food food;
public GameManager (String fileName) {
BufferedReader fileInput = null;
try {
fileInput = new BufferedReader(new FileReader(fileName));
Scanner fileScanner = new Scanner(fileInput);
int rows = fileScanner.nextInt();
int cols = fileScanner.nextInt();
board = new GameObject[rows][cols];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
board[row][col] = new Empty();
}
}
while(fileScanner.hasNext()) {
fileScanner.nextLine();
int xStart = fileScanner.nextInt();
int yStart = fileScanner.nextInt();
int xEnd = fileScanner.nextInt();
int yEnd = fileScanner.nextInt();
addWall(xStart, yStart, xEnd, yEnd);
}
addGameObject(snk);
addGameObject(food);
fileScanner.close();
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if(fileInput != null) {fileInput.close();}
} catch(IOException e) {
e.printStackTrace();
}
}
}
public void newRandomXY() {
Random r = new Random(0);
this.xR = r.nextInt(board.length);
this.yR = r.nextInt(board.length);
}
public void addGameObject(GameObject s) {
newRandomXY();
while(board[xR][yR].isOccupied()) {
newRandomXY();
}
if(s instanceof Snake) {
s = new Snake(xR, yR);
board[xR][yR] = s;
} else if(s instanceof Food) {
s = new Food(xR, yR);
board[xR][yR] = s;
}
}
public void addWall(int xStart, int yStart, int xEnd, int yEnd) {
for(int x = xStart; x <= xEnd; x++) {
for(int y = yStart; y <= yEnd; y++) {
board[x][y] = new Wall();
}
}
}
#Override
public String toString() {
String ret = "";
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
ret += board[row][col].toString();
}
ret += "\n";
}
return ret;
}
}
Now the issue I'm having is that whenever I try to print a string version of this board on my cmd, the program just hangs and I have to hard close the cmd. I have been messing around with some of the code and I have been able to fix the program just crashing, but I haven't been able to figure why its all not printing out.
Here is my Snake Class (Note: I also have some other methods in this class that I am not using at the moment, so I don't think they are the issue):
public class Snake extends GameObject {
private Point head;
private Deque<Point> snakeBody;
private int lenght = 0;
private String direction = "UP";
public Snake(int x, int y) {
this.head = super.newCell(x, y);
this.snakeBody = new ArrayDeque<Point>();
this.snakeBody.push(head);
}
and my toString of Snake:
public String toString(Deque<Point> s) {
String str = "";
for(Point p : s) {
String snk = p.toString();
snk = "S";
str += snk;
}
return str;
}
Here's my Food Class:
public class Food extends GameObject {
private Point foodLoc;
public Food(int x, int y) {
this.foodLoc = new Point(x, y);
}
public Point getLocation() {
return foodLoc.getLocation();
}
public String toString() {
return "F";
}
}
and here is my GameObject Class:
import java.awt.Point;
public class GameObject {
public final int CELL_SIZE = 1;
public Point newCell(int x, int y) {
return new Point(x, y);
}
public boolean isOccupied() {
return true;
}
public boolean isOccupied(Point p, Point o) {
boolean flag = false;
if(p.getLocation().equals(o.getLocation())) {
flag = true;
} else {
flag = false;
}
return flag;
}
}
I have a good feeling that my toString method in Snake is completely wrong, but I don't necessarily understand how to fix it and why my other toString methods don't work. I have looked around the forums to see if I could find and answer, but I couldn't find anything for this.
The problem is that you're trying to print an array of type gameObject, but that object does not have a .toString operator, so it's always going to return null.
I'm guessing that you want to represent the cells as either empty or occupied, or perhaps even further by having food, but you'd need a .toString in that class to define what you want returned in whatever given scenario.
When writing a class it is giving me an expected token error and I can not figure out how to solve it or why it is giving it to me.
Here's the code:
public class SetUpDoors {
private int DoorAmount;
private int WinningDoorAmount;
private int[] DoorArray= new int[DoorAmount];
private int winnerSelect = 0;
for (int i = 0; i < DoorAmount; i++) {
if (WinningDoorAmount > 0) {
winnerSelect = (int) Math.round( Math.random());
DoorArray[i] = winnerSelect;
if(winnerSelect == 1) {
WinningDoorAmount--;
}
}
else {
DoorArray[i] = 0;
}
DoorAmount--;
}
void setDoorAmount(int userDoors){
DoorAmount = userDoors;
}
void setWinningDoorAmount(int userWinningDoors) {
WinningDoorAmount = userWinningDoors;
}
}
it is giving the error on the ; at the end of private int winnerSelect = 0;
and an error for the } right below DoorAmount--;
The first is expected token "{" and the second is add "}" to complete block.
You must declare following code inside a method.
For example:
public void newMethod(){
for (int i = 0; i < DoorAmount; i++) {
if (WinningDoorAmount > 0) {
winnerSelect = (int) Math.round( Math.random());
DoorArray[i] = winnerSelect;
if(winnerSelect == 1) {
WinningDoorAmount--;
}
}
}
else {
DoorArray[i] = 0;
}
DoorAmount--;
}
try this
public class SetUpDoors {
private int DoorAmount;
private int WinningDoorAmount;
private int[] DoorArray= new int[DoorAmount];
private int winnerSelect = 0;
{
for (int i = 0; i < DoorAmount; i++) {
if (WinningDoorAmount > 0) {
winnerSelect = (int) Math.round( Math.random());
DoorArray[i] = winnerSelect;
if(winnerSelect == 1) {
WinningDoorAmount--;
}
}
else {
DoorArray[i] = 0;
}
DoorAmount--;
}
}
void setDoorAmount(int userDoors){
DoorAmount = userDoors;
}
void setWinningDoorAmount(int userWinningDoors) {
WinningDoorAmount = userWinningDoors;
}
}
I am creating a new mod that has a container. The issue is that the inventory slots are offset by 7. It seems like the server thinks the first column is where the second-to-last column, and the second column is where the last column is. Here is some code. The classes should be included.
public class GuiHandler implements IGuiHandler {
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
if (ID == 0) {
return new ContainerGenerator(player.inventory, (TileEntityGenerator) world.getTileEntity(x, y, z));
} else {
return null;
}
}
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
if (ID == 0) {
return new GuiGenerator(player.inventory, (TileEntityGenerator) world.getTileEntity(x, y, z));
} else {
return null;
}
}
}
The container class
public class ContainerGenerator extends Container {
private TileEntityGenerator tileEntity;
private int count;
public ContainerGenerator(InventoryPlayer inventory, TileEntityGenerator tileEntity) {
this.tileEntity = tileEntity;
this.tileEntity.openInventory();
this.count = tileEntity.getCount();
this.addSlotToContainer(new Slot(tileEntity, 0, 27, 47));
this.addSlotToContainer(new Slot(tileEntity, 1, 134, 47));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for (int i = 0; i < 9; i++) {
this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142));
}
}
// some other random code
And some relevent tile entity overrides (there is some other random code)
#Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
this.count = nbt.getInteger("count");
NBTTagList tagList = nbt.getTagList("items", 10);
this.items = new ItemStack[2];
NBTTagCompound compound = tagList.getCompoundTagAt(0);
byte slotId = compound.getByte("slot");
if (slotId >= 0 && slotId < 2) {
items[slotId] = ItemStack.loadItemStackFromNBT(compound);
}
compound = tagList.getCompoundTagAt(1);
slotId = compound.getByte("slot");
if (slotId >= 0 && slotId < 2) {
items[slotId] = ItemStack.loadItemStackFromNBT(compound);
}
}
#Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setInteger("count", count);
NBTTagList list = new NBTTagList();
if (items[0] != null) {
NBTTagCompound compound = new NBTTagCompound();
compound.setByte("slot", (byte) 0);
items[0].writeToNBT(compound);
list.appendTag(compound);
}
if (items[1] != null) {
NBTTagCompound compound = new NBTTagCompound();
compound.setByte("slot", (byte) 1);
items[1].writeToNBT(compound);
list.appendTag(compound);
}
nbt.setTag("items", list);
}
#Override
public int getSizeInventory() {
return items.length;
}
#Override
public ItemStack getStackInSlot(int slot) {
return items[slot];
}
#Override
public ItemStack decrStackSize(int slot, int amount) {
if (items[slot] == null) {
return null;
}
if (items[slot].stackSize == amount) {
ItemStack itemStack = items[slot];
items[slot] = null;
return itemStack;
} else {
ItemStack itemStack = items[slot].splitStack(amount);
if (items[slot].stackSize == 0) {
items[slot] = null;
}
markDirty();
return itemStack;
}
}
#Override
public ItemStack getStackInSlotOnClosing(int slot) {
if (items[slot] != null) {
ItemStack itemStack = items[slot];
items[slot] = null;
return itemStack;
} else {
return null;
}
}
#Override
public void setInventorySlotContents(int slot, ItemStack stack) {
items[slot] = stack;
if (stack != null && stack.stackSize > getInventoryStackLimit()) {
markDirty();
}
}
Edit: screenshots. Notice which slots are highlighted if any.
Sorry for the lengthy post. I'm new to modding, so it is hard for me to pin point the issue exactly.
First of all there is a lot of code because i really don't know where my issue is, I do apologise!
I'm making a program that solves a varying size puzzle for eg (3x3 with 0-8) The zero represents a blank tile the objective is to move the the blank tile around until the goal state is reached. At the moment I am using Depth First Search to solve the puzzle. Netbeans returns a 'OutOfMemory' error when i run the program however if I change the goal state so it only requires one move to complete it displays the solution. Below is my code, I've only added some of the classes because I don't want this post being ridiculously long. Please let me know if you require the other classes
EDIT: Inserted wrong code for board class
Depth First Search Class
package npuzzle;
import java.util.ArrayList;
import java.util.Stack;
public class DFSearch {
public static void search(int[][] initial, int[][] goal, int rows, int columns)
{
Board board = new Board(initial, goal, rows, columns, "");
Node root = new Node(board);
Stack<Node> stack = new Stack<Node>();
stack.add(root);
performSearch(stack, board);
}
public static void performSearch(Stack<Node> s, Board board)
{
int searchCount = 1;
while (!s.isEmpty())
{
Node tNode = (Node) s.pop();
//if not goal state
if (!tNode.getCurrentState().isGoalState())
{
ArrayList<State> tChildren = tNode.getCurrentState().gChildren();
for (int i = 0; i < tChildren.size(); i++)
{
Node newNode = new Node(tNode, tChildren.get(i), tNode.getGCost() + tChildren.get(i).findCost(), 0);
if(!isRepeatedState(newNode))
{
s.add(newNode);
}
}
searchCount++;
}
else
{
tNode.getCurrentState().printState();
System.exit(0);
//PRINTING OF DIRECTIONS
}
}
System.out.println("Error! No solution found!");
}
private static boolean isRepeatedState(Node c)
{
boolean returnVal = false;
Node checkNode = c;
while(c.getParent() != null && !returnVal)
{
if (c.getParent().getCurrentState().equals(checkNode.getCurrentState()))
{
returnVal = true;
}
c = c.getParent();
}
return returnVal;
}
}
Board Class
package npuzzle;
import java.util.ArrayList;
import java.util.Arrays;
public class Board implements State
{
private int PUZZLE_SIZE = 0;
private int outOfPlace = 0;
private int rows;
private int columns;
private int[][] GOAL;
private int[][] currentBoard;
private String directions = "";
public Board(int[][] initial, int[][] goal, int N, int M, String direction)
{
currentBoard = initial;
GOAL = goal;
rows = N;
columns = M;
PUZZLE_SIZE = rows*columns;
directions = direction;
setOutOfPlace();
}
#Override
public boolean isGoalState() {
if (Arrays.deepEquals(currentBoard, GOAL))
{
return true;
}
return false;
}
#Override
public ArrayList<State> gChildren() {
ArrayList<State> children = new ArrayList<State>();
int[] blanktile = getBlankTile();
int[] newblanktile = Arrays.copyOf(blanktile, blanktile.length);
if (blanktile[0] != 0) {
newblanktile[0]--;
Swap(newblanktile, blanktile, children, "up");
newblanktile = Arrays.copyOf(blanktile, blanktile.length);
}
if (blanktile[1] != 0) {
newblanktile[1]--;
Swap(newblanktile, blanktile, children, "left");
newblanktile = Arrays.copyOf(blanktile, blanktile.length);
}
if (blanktile[0] != (this.rows - 1)) {
newblanktile[0]++;
Swap(newblanktile, blanktile, children, "down");
newblanktile = Arrays.copyOf(blanktile, blanktile.length);
}
if (blanktile[1] != (this.columns - 1)) {
newblanktile[1]++;
Swap(newblanktile, blanktile, children, "right");
newblanktile = Arrays.copyOf(blanktile, blanktile.length);
}
return children;
}
#Override
public double findCost() {
return 1;
}
#Override
public void printState() {
System.out.println(directions);
}
#Override
public boolean equals(State s) {
if (Arrays.deepEquals(currentBoard, ((Board) s).getCurrentBoard()))
{
return true;
}
else
return false;
}
private void setOutOfPlace() {
int i = 0;
int j = -1;
do
{
if (j == (columns - 1)) {j = 0; i++;}
else {j++;}
if (currentBoard[i][j] != GOAL[i][j])
{
outOfPlace++;
}
} while (((i+1)*(j+1)) < PUZZLE_SIZE);
}
private int[] getBlankTile()
{
int i = 0;
int j = -1;
int[] blanktile = {0,0};
do
{
if (j == (columns - 1)) {j = 0; i++;}
else {j++;}
if (currentBoard[i][j] == 0) {
blanktile[0] = i;
blanktile[1] = j;
}
} while (((i+1)*(j+1)) < PUZZLE_SIZE);
return blanktile;
}
public int getOutOfPlace()
{
return outOfPlace;
}
private int[][] copyBoard(int[][] state)
{
int[][] returnArray = new int[rows][columns];
for (int i = 0, j = 0; i*j < PUZZLE_SIZE; i++, j++)
{
returnArray[i] = Arrays.copyOf(state[i], state[i].length);
}
return returnArray;
}
private void Swap(int[] nbt, int[] bt, ArrayList<State> children, String direction) {
int[][] cpy = copyBoard(currentBoard);
int temp = cpy[nbt[0]][nbt[1]];
cpy[nbt[0]][nbt[1]] = currentBoard[bt[0]][bt[1]];
cpy[bt[0]][bt[1]] = temp;
children.add(new Board(cpy, this.getGOAL(), this.getRows(), this.getColumns(), (this.getDirections() + direction + ", ")));
}
public int getPUZZLE_SIZE() {
return PUZZLE_SIZE;
}
public int getRows() {
return rows;
}
public int getColumns() {
return columns;
}
public int[][] getGOAL() {
return GOAL;
}
public int[][] getCurrentBoard()
{
return currentBoard;
}
public String getDirections()
{
return directions;
}
}
I need to write a java linked list which needs to be array based and sorted. So the array contains nodes which have 2 fields: the data, and the index of the next element in the list. The last element of the list needs to have an index of -1.
Can someone help how to add an element to such list. this is the code I wrote so far but does not seems to be right because I can add elements but the indexes are not right.
package listpackage;
import java.io.IOException;
public class ArrayLL {
private int MAX_CAP = 100;
private ANode[] list;
private int size;
public ArrayLL(){
list = new ANode[MAX_CAP];
list[list.length-1] = new ANode(null, -1);
for(int i = 0; i < list.length-1; i++){
list[i] = new ANode(null, i+1);
}
size = 0;
}
public void addElem(String s) throws IOException{
if(this.getSize() == 0){
ANode a = new ANode(s, -1);
list[0] = a;
}else if(size == MAX_CAP + 1){
throw new IOException("List is full");
}else{
int index = 0;
for(int i=0; i < list.length; i++){
if(list[i].getData() == null){
index = i;
break;
}
}
ANode b = new ANode(s);
list[index] = b;
if(this.getSize()==1){
if (list[index].getData().compareTo(list[0].getData()) < 0){
list[index].setLink(0);
list[0].setLink(-1);
}else{
list[index].setLink(-1);
list[0].setLink(index);
}
}else{
int i = 0;
while(list[i].getData() != null){
if(list[index].getData().compareTo(list[i].getData()) < 0){
list[index].setLink(i);
if(i>0)
list[i-1].setLink(index);
}else{
i++;
}
}
}
}
size++;
}
public ANode[] getList(){
return list;
}
public int getSize(){
return size;
}
}
class ANode{
private String data;
private int link;
public ANode(String d){
data = d;
link = -1;
}
public ANode(String d, int l){
data = d;
link = l;
}
public String getData(){
return data;
}
public int getLink(){
return link;
}
public void setLink(int l){
link = l;
}
}
It was fun to solve this tricky program... :-)... i enjoyed it...here is the working solution...I tested using various scenarios...
In order to make sure I do not change much of your code...I did not optimize the code..there are many places where code can be more simpler and readable...
import java.io.IOException;
public class ArrayLL {
public static void main(String[] args) throws IOException {
ArrayLL myList = new ArrayLL();
myList.addElem("c");
myList.addElem("b");
myList.addElem("a");
myList.addElem("d");
int i = myList.startOfListIndex;
while(myList.list[i].getLink()!=-1)
{
System.out.println(myList.list[i].getData());
i = myList.list[i].getLink();
}
System.out.println(myList.list[i].getData());
}
private int MAX_CAP = 100;
private ANode[] list;
private int size;
private int startOfListIndex = 0;
public ArrayLL() {
list = new ANode[MAX_CAP];
for (int i = 0; i < list.length; i++) {
list[i] = new ANode(null);
}
size = 0;
}
public void addElem(String s) throws IOException {
if (this.getSize() == 0) {
ANode a = new ANode(s, -1);
list[0] = a;
} else if (size == MAX_CAP + 1) {
throw new IOException("List is full");
} else {
int index = 0;
for (int i = 0; i < list.length; i++) {
if (list[i].getData() == null) {
index = i;
break;
}
}
ANode b = new ANode(s);
list[index] = b;
if (this.getSize() == 1) {
if (list[index].getData().compareTo(list[0].getData()) < 0) {
list[index].setLink(0);
list[0].setLink(-1);
startOfListIndex = index;
} else {
list[index].setLink(-1);
list[0].setLink(index);
}
} else {
int i = startOfListIndex;
int prevIndex = -1;
while (i!=-1 && list[i].getData() != null) {
if (list[index].getData().compareTo(list[i].getData()) < 0) {
list[index].setLink(i);
if(prevIndex!=-1)
list[prevIndex].setLink(index);
else
startOfListIndex = index;
break;
} else {
prevIndex = i;
i=list[i].getLink();
}
}
if(i==-1)
{
list[prevIndex].setLink(index);
}
}
}
size++;
}
public ANode[] getList() {
return list;
}
public int getSize() {
return size;
}
}
class ANode {
private String data;
private int link;
public ANode(String d) {
data = d;
link = -1;
}
public ANode(String d, int l) {
data = d;
link = l;
}
public String getData() {
return data;
}
public int getLink() {
return link;
}
public void setLink(int l) {
link = l;
}
}
This is the full solution i believe as it works for all the test i have done, but there might be something i haven't thought of. The solution includes the removeElem() method too.
package listpackage;
import java.io.IOException;
public class ArrayLL {
private int MAX_CAP = 100;
private ANode[] list;
private int size;
private int startOfListIndex = 0;
public ArrayLL() {
list = new ANode[MAX_CAP];
list[list.length-1] = new ANode(null, -1);
for(int i = 0; i < list.length-1; i++){
list[i] = new ANode(null, i+1);
}
size = 0;
}
public void addElem(String s) throws IOException {
if (this.getSize() == 0) {
ANode a = new ANode(s, -1);
list[0] = a;
}else if (size == MAX_CAP + 1) {
throw new IOException("List is full");
}else{
int index = 0;
for (int i = 0; i < list.length; i++) {
if (list[i].getData() == null) {
index = i;
break;
}
}
ANode b = new ANode(s);
list[index] = b;
if (this.getSize() == 1) {
if (list[index].getData().compareTo(list[0].getData()) < 0) {
list[index].setLink(0);
list[0].setLink(-1);
startOfListIndex = index;
} else {
list[index].setLink(-1);
list[0].setLink(index);
}
} else {
int i = startOfListIndex;
int prevIndex = -1;
while (i!=-1 && list[i].getData() != null) {
if (list[index].getData().compareTo(list[i].getData()) < 0) {
list[index].setLink(i);
if(prevIndex!=-1)
list[prevIndex].setLink(index);
else
startOfListIndex = index;
break;
}else{
prevIndex = i;
i=list[i].getLink();
}
}
if(i==-1)
{
list[prevIndex].setLink(index);
}
}
}
size++;
}
public void removeElem(String s) throws IOException {
if (this.getSize() == 0) {
throw new IOException("List is empty");
}else{
int firstEmpty = 0;
for(int i = 0; i< list.length; i++){
if(list[i].getData() == null){
firstEmpty = i;
break;
}
}
int elemindex = 0;
int prev = -1;
int next=-1;
for(int i = 0; i < list.length; i++){
if(list[i].getData() != null && list[i].getData().compareTo(s) == 0){
elemindex = i;
next = list[i].getLink();
for(int j = 0; j < list.length; j++){
if(list[j].getLink() == elemindex){
prev = j;
break;
}
}
list[elemindex].setDataNull();
list[elemindex].setLink(firstEmpty);
if(next != -1)
list[prev].setLink(next);
else
list[prev].setLink(-1);
size--;
}else{
elemindex++;
}
}
}
}
public ANode[] getList() {
return list;
}
public int getSize() {
return size;
}
}
class ANode {
private String data;
private int link;
public ANode(String d) {
data = d;
link = -1;
}
public ANode(String d, int l) {
data = d;
link = l;
}
public String getData() {
return data;
}
public void setDataNull(){
this.data = null;
}
public int getLink() {
return link;
}
public void setLink(int l) {
link = l;
}
}