Why isnt my code executing after the for loop? - java

while(counter != this.data.size()) {
for(int i=0; i <= p.data.size(); i++) {
double coefficient = first.getData().getCoefficient() * sec.getData().getCoefficient();
int degree = first.getData().getDegree() + sec.getData().getDegree();
Term current = new Term(coefficient,degree);
if(ans.data.isEmpty()) {
ans.data.addFirst(current);
}
else {
boolean found = false;
DNode<Term > tmp = ans.data.getFirst();
while(found != true) {
if(tmp.getData() == null) {
ans.data.addLast(current);
found = true;
}
else if(current.getDegree() > tmp.getData().getDegree()) {
ans.data.addBefore(current, tmp);
found = true;
}
else if(current.getDegree() == tmp.getData().getDegree()) {
double co = current.getCoefficient() * tmp.getData().getCoefficient();
int deg = current.getDegree() + tmp.getData().getDegree();
tmp.getData().setCoefficient(co);
tmp.getData().setDegree(deg);
found = true;
}
System.out.println("DID I GET HERE");
tmp = tmp.getNext();
}
System.out.println("DID I GET HERE2");
}
System.out.println("DID I GET HERE3");
sec = sec.getNext();
}
System.out.println("DID I GET HERE4");
first = first.getNext();
counter++;
}
My code doesnt even reach the did i get here4 for some reason can anyone tell me why?
I am having a lot of difficulty as this project is due later today the code is suppose to multiply 2 polynomials

Related

Head First Java book battleship game

I am reading "Head First Java" book and I came across the problem in chapter 5 with the battleship game (with simple version). I knew that the book's code doesn't work and I tried my self fixing it, but it still didn't work.
So tried to google it and I found some post on this website but I still have a problem. The game isn't working properly as it should.
If a player enters any random number, the output is always "hit"...
This is the last version of the code:
DotCom class:
public class DotCom {
private ArrayList<String> locationCells = new ArrayList<>();
public void setlocationCells(int[] loc) {
if (loc != null)
for (int val : loc)
locationCells.add(String.valueOf(val));
}
public String checkYourself(String userInput) {
String result = "miss";
int index = locationCells.indexOf(userInput);
if (index >= 0) {
locationCells.remove(index);
}
if (locationCells.isEmpty()) {
result = "kill";
} else {
result = "hit";
}
System.out.println(result);
return result;
}
}
DotComGame class:
public class DotComGame {
public static void main(String[] args) {
int guessingTimes = 0;
DotCom dot = new DotCom();
GameHelperrr helper = new GameHelperrr();
int randomNum = (int) (Math.random() * 5);
int[] locations = { randomNum, randomNum + 1, randomNum + 2 };
dot.setlocationCells(locations);
boolean isAlive = true;
while (isAlive == true) {
String guess = helper.getUserInput("Enter a number");
String result = dot.checkYourself(guess);
guessingTimes++;
if (result.equals("kill")) {
isAlive = false;
System.out.println("You took " + guessingTimes + " guesses");
}
}
}
}
I would really appreciate to get a detailed and understandable answer, because I'm stuck and I couldn't move on with the book for a few days now.
int index = locationCells.indexOf(userInput);
This method will return -1 if the element doesn't exist in the collection.
So if you miss, it won't hit this condition:
if (index >= 0) {
locationCells.remove(index);
}
There are still elements in this collection because you didn't remove anything...
if (locationCells.isEmpty()) {
result = "kill";
} else {
result = "hit";
}
So on a miss, the result still shows "hit."
Try this instead:
if (locationCells.isEmpty()) {
result = "kill";
} else {
result = index == -1 ? "miss" : "hit";
}
If you haven't killed the opponents ships, then you either miss all ships or you hit a single ship.
I would guess the checkYourself-Method must be like this:
public String checkYourself(String userInput) {
String result = "miss";
int index = locationCells.indexOf(userInput);
if(index >= 0) {
locationCells.remove(index);
if (locationCells.isEmpty()) {
result = "kill";
}else {
result = "hit";
}
}
System.out.println(result);
return result;
}
In it's current form the ArrayList is never empty because you insert 3 Values but only remove 1 if the user-input is in the list so .isEmpty() is never TRUE.

Java maze won't print

I have to make a maze for a java assignment, and I was able to finish most of it. I was provided with an outline of the code that had all the methods. Can someone help me? My issue is that the maze wont print out, and I can't figure out why.
package maze;
public class Maze {
private char direction;
private int r; // x position of the mouse
private int c; //y position of the mouse
private boolean exitFound = false;
public Maze(int[][] arrMaze) {
this.r = arrMaze.length - 1;
this.c = 0;
}
//Prints out the maze without solution
public void displayMaze(int[][] arrMaze)
{
//display the maze putting blank spaces where there are 1's in the array and putting
//another symbol where there are 0's to show the maze without the solution
for(int i=0; i<arrMaze.length; i++){
System.out.println(" ");
for(int j=0; j<arrMaze[i].length; j++){
if(arrMaze[i][j] == 0){
System.out.print("#");
} if(arrMaze[i][j] == 1) {
System.out.print(" ");
} if(arrMaze[i][j] == 2){
System.out.print("#");
} if(arrMaze[i][j] == 3){
System.out.println("~");
}
}
}
}
//displays the Maze with the path taken
public void displayPath(int[][] arrMaze)
{
//show the user how far the mouse has gone since the start.
//The path the mouse has gone will be filled in but the path ahead will not.
for (int i = 0; i < arrMaze.length; i++) {
System.out.println(" ");
for (int j = 0; j < arrMaze[i].length; j++) {
if (arrMaze[i][j] == 3) {
System.out.print("#");
} else if (arrMaze[i][j] == 2) {
System.out.print("~");
} else if (arrMaze[i][j] == 0) {
System.out.print("#");
} else {
}
}
}
}
public boolean takeStep(int[][] newMaze) {
// moveNorth(newMaze)
for (int i = 0; i < newMaze.length; i++) {
System.out.println(" ");
for (int j = 0; j < newMaze[i].length; j++) {
if (newMaze[r][c] == 3) {
moveNorth(newMaze);
System.out.print("~");
} else if (newMaze[r][c] == 2) {
System.out.print("#");
} else {
}
}
}
return isAnExit(newMaze);
}
public void moveNorth(int[][] arrMaze) {
//complete the code here
/*method will check for a 0 or a 1 in the position above the current position
* and then if not a 0 will change the current position to the row above it, but in the same column.
*/
if (arrMaze[r][c - 1] != 0) {
arrMaze[r][c - 1] = 3;
arrMaze[r][c + 1] = 2;
} else {
moveSouth(arrMaze);
}
displayPath(arrMaze);
}
public void moveSouth(int[][] arrMaze)
{
//method will check for a 0 or a 1 in the position below the current position and then if not a 0
//it will change the current position to the row below it, but in the same column.
if (arrMaze[r][c + 1] != 0) {
arrMaze[r][c + 1] = 3;
arrMaze[r][c + 1] = 2;
} else {
moveNorth(arrMaze);
}
displayPath(arrMaze);
}
public void moveEast(int[][] arrMaze) {
//method will check for a 0 or a 1 in the position to the right of  the current position and then if
//not a 0 will change the current position to the column to the right but the same row.
if (arrMaze[r + 1][c] != 0) {
arrMaze[r + 1][c] = 3;
arrMaze[r - 1][c] = 2;
} else {
moveWest(arrMaze);
}
displayPath(arrMaze);
}
public void moveWest(int[][] arrMaze) {
//method will check for a 0 or a 1 in the position to the left of  the current position and then if
//not a 0 will change the current position to the column to the left but the same row.
if (arrMaze[r - 1][c] != 0) {
arrMaze[r - 1][c] = 3;
arrMaze[r + 1][c] = 2;
} else {
}
displayPath(arrMaze);
}
private boolean isAnExit(int[][] arrMaze) {
//method will return true if the user arrives into the last column of the array because there is only one
//location in the last column that is a 1, so if the user reaches the array[i].length then that means that it found an exit.
if (arrMaze[r][c] > arrMaze.length) {
exitFound = true;
} else {
exitFound = false;
}
return exitFound;
}
//finds the path without stopping at every step
//method will show the complete path from start to finish of the maze and the suggested route to the end.
public void findExit(int[][] arrMaze) {
if (arrMaze[r][c] > arrMaze.length) {
for (int i = 0; i < arrMaze.length; i++) {
takeStep(arrMaze);
}
}
}
}
This is the test code. I was provided the test code, and I haven't changed it.
package maze;
import java.util.Scanner;
public class TestMaze
{
public static void main(String[] args)
{
int[][] mazeArray = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
{0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1},
{0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0},
{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
Maze myMaze = new Maze(mazeArray);
boolean keepAsking = true;
Scanner scan = new Scanner(System.in);
String input = "";
myMaze.displayPath(mazeArray);
System.out.println("Maze");
do {
System.out.println("T = Take a step | S = Show path | Q = Quit");
System.out.print("Enter command: ");
input = scan.nextLine();
input.trim();
input.toLowerCase();
if(input.equals("t")) {
keepAsking = !myMaze.takeStep(mazeArray);
System.out.println("Which direction would you like to go? N, S, E, W?");
String direction = scan.nextLine();
if(direction.equalsIgnoreCase("n"))
myMaze.moveNorth(mazeArray);
if(direction.equalsIgnoreCase("s"))
myMaze.moveSouth(mazeArray);
if(direction.equalsIgnoreCase("e"))
myMaze.moveEast(mazeArray);
if(direction.equalsIgnoreCase("w"))
myMaze.moveWest(mazeArray);
}
else if(input.equals("s")) {
myMaze.findExit(mazeArray);
keepAsking = false;
}
else if(input.equals("q")) {
keepAsking = false;
}
else {
System.out.println("ERR: Invalid input");
}
} while(keepAsking);
System.out.println("Quitting program...");
scan.close();
}
}
You need to call displayMaze() (in displayPath()) to print it.
Currently, you're not calling the method, meaning that your code will never print the maze as it's no being instructed to.
Also, where are you assigning values to r and c? I think you meant to use i and j in your displayPath() method ([i][j] instead of [r][c]).
I imagine you're throwing an error somewhere because your r and c values are undefined everywhere they are used. Try adding code to initialize and update these values, then you should see your maze print.

binary search between two user defined points, returning all names inbetween those two entered

In java, I have to do a binary search, asking the user to select two names that exist in my list and then print all the people between those two, starting at the first name they enter and ending at the second name they enter. I start by calling the method searchTwoPeople. It's returning nothing.... Here's my code:
public int binarySearch(String searchItem)
{
int first = 0;
int last = nMembers - 1;
int mid = 0;
boolean found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (bookMembers[mid].lastName.compareTo(searchItem) == 0)
found = true;
else if (bookMembers[mid].lastName.compareTo(searchItem) > 0)
last = mid - 1;
else
first = mid + 1;
}
if (!found)
mid = -1; //it is an unsuccessful search
return mid;
}//end binarySearch
public int findFirstPosition(int position)
{
int newPosition = position;
if (position == 0)
{
return position;
}
while (true)
{
if (bookMembers[newPosition-1].lastName.compareTo(bookMembers
[position].lastName) != 0)
{
return newPosition;
}
else
{
--newPosition;
if (newPosition == 0)
{
return newPosition; // no more people in list
}
}
// end sequential search
}
}
public void searchTwoPeople()
{
String lastName = new String();
String lastName2 = new String();
int position, nextPosition;
int tryCount = 0;
boolean more = true;
do
{
lastName = inputFirstLastName();//this is the
method where I have user input a name where to start search and it's passed in here
lastName2 = inputSecondLastName(); //user puts
name where to end search
position = binarySearch(lastName);
if (position == -1)
{
System.out.println ("\n You have no
contacts with the name " + lastName);
++ tryCount;
}
}
while (position == -1 && tryCount < 3);// only let them try
3 times....
if (tryCount == 3)
{
System.out.println("Only allowed 3 attempts -
select a new menu option");
return;
}
nextPosition = findFirstPosition(position);
while (more)
{
System.out.println(bookMembers
[nextPosition].toString());
}
++ nextPosition; // check next person
if (nextPosition == nMembers) // last person in
the list
{
more = false;
}
else
{
if (bookMembers
[nextPosition].lastName.compareTo(lastName2) == 0)
{
more = false;
}
}
}

How to find Largest Prime Number, Smallest Factor except 1, Sum of Numbers

Herro, Um I'm not sure if this is how you use this site but uh lets get to it... So I need help on this project and I have to do this -
Input [][] Output
A - F -> Multiply by 3, Divide by 4
G - J -> Divide by 3 + 25
K - N -> Find Greatest Factor * 2
O - Q -> Find largest prime inclusive * 3
R - W -> Find Smallest Factor Except 1
X - Z -> Sum Numbers
So I was wondering if my first couple is correct, and need help on the empty space. So the Letter represents the number of the as in "Z" is the last letter so its 26, and "A" is the first so its 1. So if an responses... Thanks ! package Fun;
import java.util.Scanner;
public class Fun {
public static void main(String[] args) {
// TODO Auto-generated method stub
run();
}
public static void run()
{
input();
evaluateAlphabet();
evaluate();
}
public static void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Letter, please");
x = sc.next();
}
public static String x = " ";
public static int temp = 0;
public static int answer = 0;
public static void evaluateAlphabet()
{
if(x.equals("A"))
{
temp = 1;
}
else if (x.equals("B"))
{
temp = 2;
}
else if (x.equals("C"))
{
temp = 3;
}
else if (x.equals("D"))
{
temp = 4;
}
else if (x.equals("E"))
{
temp = 5;
}
else if (x.equals("F"))
{
temp = 6;
}
else if (x.equals("G"))
{
temp = 7;
}
else if (x.equals("H"))
{
temp = 8;
}
else if (x.equals("I"))
{
temp = 9;
}
else if (x.equals("J"))
{
temp = 10;
}
else if (x.equals("K"))
{
temp = 11;
}
else if (x.equals("L"))
{
temp = 12;
}
else if (x.equals("M"))
{
temp = 13;
}
else if (x.equals("N"))
{
temp = 14;
}
else if (x.equals("O"))
{
temp = 15;
}
else if (x.equals("P"))
{
temp = 16;
}
else if (x.equals("Q"))
{
temp = 17;
}
else if (x.equals("R"))
{
temp = 18;
}
else if (x.equals("S"))
{
temp = 19;
}
else if (x.equals("T"))
{
temp = 20;
}
else if (x.equals("U"))
{
temp = 21;
}
else if (x.equals("V"))
{
temp = 22;
}
else if (x.equals("W"))
{
temp = 23;
}
else if (x.equals("X"))
{
temp = 24;
}
else if (x.equals("Y"))
{
temp = 25;
}
else if (x.equals("Z"))
{
temp = 26;
}
else if (x.equals("Qwerty"))
{
temp = 27;
}
}
public static void evaluate()
{
if(temp>=1 && temp<= 6)
{
answer = (temp * 3)/4;
System.out.println("Answer is " + answer);
}
else if(temp >= 7 && temp<= 10)
{
answer = (temp/3) + 25;
System.out.println("Answer is " + answer);
}
else if(temp >= 11 && temp<= 14)
{
}
else if(temp>=15 && temp<= 17)
{
for(int i = temp; i>0; i--)
{
for(int j = 2; j <=i/2 + 1; j++)
{
if(i%j==0)
{
break;
}
if(j==i/2 + 1)
{
answer = i * 3;
}
}
}
System.out.println("Answer is " + answer);
}
else if(temp>=18 && temp<= 23)
{
answer = temp;
}
else if(temp>= 24 && temp<=26)
answer = (answer * 12)%26;
System.out.println("Answer is " + answer);
}
}
-Corruption
Here is a better approach for char to int conversion:
Assuming the string has at least 1 character(check for its length), you can get the temp by doing:
temp = x.getCharAt(0) - 'A' + 1;
or, safety first:
temp = 0;
if (x.matches("^[A-Z]{1}$") {
temp = x.getCharAt(0) - 'A' + 1;
}
What's happening here? Every character has an ASCII code, an integer. So, when you have 2 chars and you try to get the distance between them, the result is an int. 'A' - 'A' = 0(that's why i added a + 1), 'B' - 'A' = 1 and so on.
For the if condition, I am using a RegExp. ^ means start of the input, [A-Z]{1} means one of A-Z, $ means the end of the input. So, if it's an A-Z, temp will get a value, anything else won't make it in the if and your temp will remain 0 so you can easily test if you've got an A-Z or not.
That's all for code review, I won't give you solutions, you must work harder, use Google. You won't enjoy and learn if I give you everything ready for a copy paste.

java object comparison [duplicate]

This question already has answers here:
Compare objects in LinkedList.contains()
(6 answers)
Closed 9 years ago.
I'm trying to check if an object exists within a linked list, and perform an action depending on if it exists or not, however, java is treating all the objects as different no matter what I do. The main code is provided below, and I'm pretty sure the error in the logic is in this code. The article and customer classes are very standard. The flag variable, which is supposed to be true if the list contains the article with the title, is always false. Any help would be much appreciated.
import java.util.*;
import java.io.*;
public class Proj1 {
public static void main(String[] args) throws FileNotFoundException {
LinkedList<Article> Articles = new LinkedList<Article>();
LinkedList<Customer> Customers = new LinkedList<Customer>();
ListIterator<Customer> it = Customers.listIterator();
int id = 0;
String command = "";
if (args.length == 0 || args[0] == null) {
System.out.println("Please give a valid command file");
} else {
try {
Scanner reader = new Scanner(new FileInputStream(args[0]));
while (reader.hasNext()) {
String arg = reader.nextLine();
arg.split(" ");
String[] commands = arg.split("\\s+");
if (isInt(commands[0])) {
id = Integer.parseInt(commands[0]);
command = commands[1];
Customer temp = new Customer(id);
if (Customers.size() == 0) {
Customers.add(temp);
} else {
boolean flag = false;
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
flag = true;
}
}
if (flag == false) {
Customers.add(temp);
}
}
} else {
command = commands[0];
}
// System.out.println(id+" "+command);
if (command.equalsIgnoreCase("borrow")) {
String title = "";
int x = commands.length;
boolean flag = false;
for (int j = 2; j < x; j++) {
title += commands[j] + " ";
}
Article Article = new Article(title);
System.out.println(Articles.size());
if (Articles.size() == 0) {
Articles.add(Article);
} else {
for (int i = 0; i < Articles.size(); i++) {
if (Article.getTitle() == Articles.get(i).getTitle()) {
flag = true;
}
}
if (flag == false) {
Articles.add(Article);
}
}
System.out.println(flag);
for (int i = 0; i < Customers.size(); i++) {
if (Customers.get(i).CustomerList().contains(title) && flag == true) {
Article.addToQ(Customers.get(i));
} else {
Customers.get(i).CustomerBorrow(Article);
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("return")) {
String title = "";
int x = commands.length;
for (int j = 2; j < x; j++) {
title += commands[j] + " ";
}
Article Article = new Article(title);
if (Articles.size() == 0) {
Articles.add(Article);
} else {
boolean flag = false;
for (int i = 0; i < Articles.size(); i++) {
if (title == Articles.get(i).getTitle()) {
flag = true;
}
}
if (flag == false) {
Articles.add(Article);
}
}
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
Customers.get(i).CustomerReturn(Article);
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("list")) {
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
System.out.println("Customer " + id
+ " currently has: "
+ Customers.get(i).CustomerList());
}
}
} else if (command.equalsIgnoreCase("whohas")) {
String title = "";
int x = commands.length;
for (int i = 1; i < x; i++) {
title += commands[i] + " ";
}
boolean flag = false;
int tempId = 0;
for (int i = 0; i < Customers.size(); i++) {
tempId = Customers.get(i).getId();
if (Customers.get(i).CustomerList().contains(title)) {
flag = true;
tempId = Customers.get(i).getId();
}
}
if (flag = true) {
System.out.println(tempId + " currently has "
+ title);
} else {
System.out
.println("Currently no one has checked out "
+ title);
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("waitlist")) {
String title = "";
int x = commands.length;
for (int i = 1; i < x; i++) {
title += commands[i] + " ";
}
for (int i = 0; i < Customers.size(); i++) {
if (Customers.get(i).CustomerList().contains(title)) {
Articles.get(i).printQ();
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("listCustomers")) {
System.out.println("Customers include: ");
for (int i = 0; i < Customers.size(); i++) {
System.out.println(Customers.get(i).getId());
}
} else {
System.out.println("Command not recognized");
}
}
reader.close();
}
catch (Exception e) {
System.out.println("command not formatted correctly");
}
}
}
public static boolean isInt(String string) {
try {
Integer.parseInt(string);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
}
commands such as
29 borrow "new york times"
29 borrow "new york times"
allow duplicates, and I'm trying to avoid this. Thanks.
Could it be that
if (Article.getTitle() == Articles.get(i).getTitle()) {
intends to compare strings? That would explain why your flag always comes back false. To compare strings in Java you should use equals (or equalsIgnoreCase for case-insensitive comparison)
if (Article.getTitle().equals(Articles.get(i).getTitle()) {
More background information here
I'm only guessing, but I'd bet that you either didn't override equals and hashCode in your Customer and Article classes or you didn't do it properly.
Joshua Bloch shows you how in Chapter 3 of "Effective Java".
I'd also wonder why you didn't choose the Set data structure if duplicates weren't allowed.

Categories

Resources