我目前正在做一个代码,试图将一个单独的LinkedList.java类实现到我的主驱动程序类。
在执行此操作后,我将显示输出并要求用户删除存储在链表中的一个元素,然后再次显示输出以显示更改。
问题 如何实现或使用LinkedList.Java与我的主驱动程序类来获取然后使用remove方法?我还需要使用add方法吗? 我只是不知道格式的样子。尝试粘贴LinkedList类但我收到错误。
这是我的代码:
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.io.*;
import java.util.LinkedList;
import java.io.InputStreamReader;
public class TestingCode {
//private List<HawaiiNativeForestBirds> listofBirds = new LinkedList<HawaiiNativeForestBirds>();
//private String database = new String("birds3.csv");
/**
* The main method starts the program
*
*@param args is the input file
*/
public static void main(String[] args) {
//error checking for commandline input
if(args.length != 1){
System.out.println("Please enter at least one input file into the argument.");
//terminates the program if more than 1 is entered
System.exit(1);
}
String csvFile = args[0];
String line = "";
String cvsSplitBy = ",";
List<HawaiiNativeForestBirds> listofBirds = new LinkedList<HawaiiNativeForestBirds>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
String[] bird = line.split(cvsSplitBy);
HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],bird[2],Integer.valueOf(bird[3]));
listofBirds.add(Hawaiinbird);
}
}
catch (IOException e) {
e.printStackTrace();
}
// Display the actual values
HawaiiNativeForestBirds[] hbirds=new HawaiiNativeForestBirds[listofBirds.size()];
int i=0;
hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
System.out.println("Display HawaiiNativeForestBirds linked list after adding objects from the input file into a linked list:");
System.out.println("index " + "name "+ " Scientific Name "+ " Color " + " Population");
i=0;
for (HawaiiNativeForestBirds hbird:hbirds){
i++;
System.out.println(i+" "+hbird.toString());
}
//remove method that asks the user for input on which row to delete
// get user input
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
String input;
int num;
Scanner sc = new Scanner(System.in);
while (( input = br.readLine()) != null){
System.out.print("Enter the row number of the item you wish to delete: ");
num=sc.nextInt();
if (num <= 0) {
System.out.println("ERROR: The row number cannot be negative or zero.");
}
// check for row number too big
else if (num > listofBirds.size() + 1) {
System.out.println("ERROR: The row number is too big for the list.");
}
else {
for(int x=0;x<num;i++) {
listofBirds.delete(num);
}//for
}//else
}//while
}
catch (IOException e) {
e.printStackTrace();
}
// Display values again with the deleted row of data
hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
System.out.println("Display HawaiiNativeForestBirds linked list after deleting a row of objects from the linked list:");
System.out.println("index " + "name "+ " Scientific Name "+ " Color " + " Population");
i=0;
for (HawaiiNativeForestBirds hbird:hbirds){
i++;
System.out.println(i+" "+hbird.toString());
}
} // end of main
} //end of class
/**
* Class HawaiianNativeForestBirds stores and displays the data for each HawaiianTheme object
*
*
*/
class HawaiiNativeForestBirds {
private String name;
private String scientificname;
private String color;
private Integer population;
public HawaiiNativeForestBirds(){
}
//constructor - used to initialize the four data fields
/**
* Stores the name,scientific name, color and population of the Hawaiian Birds
*
*
* @param name is the name of the birds from the linked list
* @param scientificname is the scientific name of the birds in the linked list
* @param color is the color of each of the birds in the linked list
* @param population is the total number of birds in the linked list
*/
public HawaiiNativeForestBirds(String name, String scientificname,
String color, Integer population) {
super();
this.name = name;
this.scientificname = scientificname;
this.color = color;
this.population = population;
}
/**
* Gets each bird's name
*
* @return the birds name
*/
public String getName() {
return name;
}
/**
* Sets each birds name
*
* @param name is the bird's name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the bird's scientific name
*
* @return the bird's scientific name
*/
public String getScientificname() {
return scientificname;
}
/**
* Sets the birds scientific name
*
* @param scientificname is the bird's scientific name
*/
public void setScientificname(String scientificname) {
this.scientificname = scientificname;
}
/**
* Gets the bird's color
*
* @return the bird's color
*/
public String getColor() {
return color;
}
/**
* Sets the bird's color
*
* @param color is the bird's color
*/
public void setColor(String color) {
this.color = color;
}
/**
* Gets the bird's population
*
* @return total population of the bird
*/
public Integer getPopulation() {
return population;
}
/**
* Sets the bird's population
*
* @param population is the total population of the bird
*/
public void setPopulation(Integer population) {
this.population = population;
}
/**
* Display the output
*
* @return the name, scientificname, color, and population of the bird
*/
public String toString() {
String output = name +" "+ scientificname + " "+ color +" "+ population;
return output;
}//end of toString
}// end of class
&#13;
这是我的LinkedList.java,ListInterface.java,Node.Java和ListException.java,我将尝试将其实现到我的主驱动程序,这些驱动程序都是独立的。
public class LinkedList<T> implements ListInterface<T> {
// reference to first node in the linked list (linked nodes)
protected Node<T> head = null;
// Total number of items, which is used
// for error checking and node removal.
protected Integer size = new Integer(0);
public LinkedList() {
// no code, because data fields already initialized
}
/**
* Adds an item (of any class) to the end of the list
*
* @param item
* is the object that is added to the list
*/
public void add(T item) {
// case 1: if empty list
if (head == null) {
// list is empty, so add to beginning of list
// make new node and assign to head of list
head = new Node<T>(item, null);
}
// if not empty list
else {
// case2: add to end of list
// current starts at 2nd node in list
Node<T> previous = head;
Node<T> current = head.getNext();
// while not at end of list
while (current != null) {
// advance to next node
previous = current;
current = current.getNext();
}
// Add new node to end of list:
// Make new node that has "null" for next.
// A node with "null" is always the last node
Node<T> node = new Node<T>(item, null);
// Point previous node (last node) to new node
previous.setNext(node);
}
// increase size of list
size++;
}
/**
* Gets an item (address to an item) from any position in the list.
*
* @param position
* The position of an item in the list.
* @returns the address to the requested item
* @exception ListException
* if an item does not exist at that position
*/
public T get(Integer position) throws ListException {
// check if empty list
if (head == null) {
throw new ListException("Cannot get an item from an empty list!");
}
// if position is outside range, throw exception
if (position < 1 || position > size) {
throw new ListException(position + " is outside list range!");
}
// Find node:
// counter to keep track of loops
Integer counter = new Integer(1);
// point to current node
Node<T> current = head;
while (!counter.equals(position)) {
// BAD CODE: while(counter != position){
// goto next node for current pointer
current = current.getNext();
// add 1 to counter
counter++;
}
// return the data (item) stored by the node
return current.getData();
}
/**
* Removes an item at any position from the list.
*
* @param position
* The position of an item in the list.
* @exception ListException
* if an item does not exist at that position
*/
public void remove(Integer position) throws ListException {
// check if empty list
if (head == null) {
throw new ListException("cannot remove from empty list");
}
// if position is outside range, throw exception
if (position < 1 || position > size) {
throw new ListException(position + " is outside list range.");
}
// if at beginning of list
if (position.equals(1)) {
// remove 1st node
head = head.getNext();
}
// if not at beginning of list
else {
// Find node:
// point previous to 1st node
Node<T> previous = head;
// point current to 2nd node
Node<T> current = head.getNext();
// loop position-2 number of times
for (int i = 2; i < position; i++) {
// goto next node for previous and current
previous = current;
current = current.getNext();
}
// Point the previous node to node after current node.
// This "skips" over one node, thus removing it!
previous.setNext(current.getNext());
}
// decrease size of list
size--;
}
/**
* Automatically called by println() or print()
*
* @return a String of the List in CSV (comma separated values) format
*/
public String toString() {
// instantiate empty string
String csvFormat = new String("");
// display position of each item to user
Integer position = new Integer(1);
// loop through all the nodes in linked list
for (Node<T> current = head; current != null; current = current
.getNext()) {
// keep adding to end of string
csvFormat = csvFormat + position + ", " + current.toString() + "\n";
// add one to position for each loop
position++;
}
return csvFormat;
}
/**
* This Is An "Accessor" Method - Used To Get A Data Field.
*
* @return the size of the list
*/
public Integer getSize() {
return size;
}
}// end of class
&#13;
public interface ListInterface<T> {
/**
* Adds an item (of any class) to the list
*
* @param item
* is the object that is added to the list
*/
public void add(T item);
/**
* Gets an item (address to an item) from the list.
*
* @param position
* The position of an item in the list.
* @returns the address to the requested item
* @exception ListException
* if an item does not exist at that position
*/
public T get(Integer position) throws ListException;
/**
* Removes an item from a list.
*
* @param position
* The position of an item in the list.
* @exception ListException
* if an item does not exist at that position
*/
public void remove(Integer position) throws ListException;
/**
* This Is An "Accessor" Method - Used To Get A Data Field.
*
* @return the size of the list
*/
public Integer getSize();
}// end of interface
&#13;
public class Node<T> {
// data fields (reference variables)
// data stores an object of any class
private T data;
// next points to the next node
private Node<T> next;
/**
* Constructor - Used To Create EAch Object & Initialize DAta Fields.
*
* @param data2
* initializes the data reference variable.
* @param next2
* initializes the next reference variable..
*/
public Node(T data2, Node<T> next2) {
data = data2;
next = next2;
}
/**
* Used to Display The Data Stored In EAch Node.
*
* @return a String for the data
*/
public String toString() {
return data.toString();
}
/**
* This Is An "Accessor" Method - Used To Get A Data Field.
*
* @return the data
*/
public T getData() {
return data;
}
/**
* This Is An "Accessor" Method - Used To Get A Data Field.
*
* @return the address to the next node
*/
public Node<T> getNext() {
return next;
}
/**
* This Is A "Mutator" Method - Used To Set A Data Field.
*
* @param data2
* is a pointer to an object.
*/
public void setData(T data2) {
data = data2;
}
/**
* This Is A "Mutator" Method - Used To Set A Data Field.
*
* @param next2
* is a pointer to the next node.
*/
public void setNext(Node<T> next2) {
next = next2;
}
} // end of class
&#13;
public class ListException extends RuntimeException {
/**
* @param message
* describes the exact cause of the error.
*/
public ListException(String message) {
super(message);
}// end of constructor
}// end of class
&#13;
希望有人可以帮助我解决这个问题:(