我怎样才能附加到这个 java 链表?
How can I append to this java linkedlist?
我需要创建一个链表。我需要向 class UnsortedBulbList 添加一个方法以附加到链表。但是,我不断收到 error:Cannot 查找符号。我已经坚持这个太久了...任何帮助将不胜感激!
'''
import java.util.Objects;
public class Bulb{
private String manufacturer;
private String partNumber;
private int wattage;
private int lumens;
public Bulb(String m, String p, int w, int l){
manufacturer = m;
partNumber = p;
wattage = w;
lumens = l;
}
public String getManufacturer(){
return manufacturer;
}
public String partNumber(){
return partNumber;
}
public int wattage(){
return wattage;
}
public int lumens(){
return lumens;
}
public void setManufacturer(String m){
if (m==null){
throw new IllegalArgumentException("Bad manufacturer");
}
manufacturer = m;
}
public void partNumber(String p){
if (p==null){
throw new IllegalArgumentException("Bad partNumber");
}
partNumber = p;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Bulb)) return false;
Bulb bulb = (Bulb) o;
return wattage == bulb.wattage &&
lumens == bulb.lumens &&
manufacturer.equals(bulb.manufacturer) &&
partNumber.equals(bulb.partNumber);
}
@Override
public int hashCode() {
return Objects.hash(manufacturer, partNumber, wattage, lumens);
}
public void wattage(int w){
if (w<0){
throw new IllegalArgumentException("Bad wattage");
}
wattage = w;
}
@Override
public String toString() {
return "Bulb{" +
"manufacturer='" + manufacturer + '\'' +
", partNumber='" + partNumber + '\'' +
", wattage=" + wattage +
", lumens=" + lumens +
'}';
}
public void lumens(int l){
if (l<0){
throw new IllegalArgumentException("Bad manufacturer");
}
lumens = l;
}
}
public class BulbList {
BulbNode first;
BulbNode last;
private int length;
public BulbList () {
BulbNode ln = new BulbNode();
first = ln;
last = ln;
length = 0;
}
public void append (Bulb s) {
BulbNode n = new BulbNode(s);
last.next = n;
last = n;
length++;
}
// public LinkedListIterator reset() {
// return new LinkedListIterator(first.next);
// }
}
public class UnsortedBulbList extends BulbList{
public UnsortedBulbList(){
}
void add(Bulb u){
u.append();
}
}
'''
当我 运行 它时,我得到错误:
找不到标志
symbol:method追加
位置:灯泡类型的变量 u
此处的错误是您正在尝试使用不存在的灯泡 class 的 append
方法。相反,我认为您想调用父 class 的 append 方法,例如:
public class UnsortedBulbList extends BulbList{
public UnsortedBulbList(){
}
void add(Bulb u){
super.append(u);
}
}
我需要创建一个链表。我需要向 class UnsortedBulbList 添加一个方法以附加到链表。但是,我不断收到 error:Cannot 查找符号。我已经坚持这个太久了...任何帮助将不胜感激!
'''
import java.util.Objects;
public class Bulb{
private String manufacturer;
private String partNumber;
private int wattage;
private int lumens;
public Bulb(String m, String p, int w, int l){
manufacturer = m;
partNumber = p;
wattage = w;
lumens = l;
}
public String getManufacturer(){
return manufacturer;
}
public String partNumber(){
return partNumber;
}
public int wattage(){
return wattage;
}
public int lumens(){
return lumens;
}
public void setManufacturer(String m){
if (m==null){
throw new IllegalArgumentException("Bad manufacturer");
}
manufacturer = m;
}
public void partNumber(String p){
if (p==null){
throw new IllegalArgumentException("Bad partNumber");
}
partNumber = p;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Bulb)) return false;
Bulb bulb = (Bulb) o;
return wattage == bulb.wattage &&
lumens == bulb.lumens &&
manufacturer.equals(bulb.manufacturer) &&
partNumber.equals(bulb.partNumber);
}
@Override
public int hashCode() {
return Objects.hash(manufacturer, partNumber, wattage, lumens);
}
public void wattage(int w){
if (w<0){
throw new IllegalArgumentException("Bad wattage");
}
wattage = w;
}
@Override
public String toString() {
return "Bulb{" +
"manufacturer='" + manufacturer + '\'' +
", partNumber='" + partNumber + '\'' +
", wattage=" + wattage +
", lumens=" + lumens +
'}';
}
public void lumens(int l){
if (l<0){
throw new IllegalArgumentException("Bad manufacturer");
}
lumens = l;
}
}
public class BulbList {
BulbNode first;
BulbNode last;
private int length;
public BulbList () {
BulbNode ln = new BulbNode();
first = ln;
last = ln;
length = 0;
}
public void append (Bulb s) {
BulbNode n = new BulbNode(s);
last.next = n;
last = n;
length++;
}
// public LinkedListIterator reset() {
// return new LinkedListIterator(first.next);
// }
}
public class UnsortedBulbList extends BulbList{
public UnsortedBulbList(){
}
void add(Bulb u){
u.append();
}
}
'''
当我 运行 它时,我得到错误: 找不到标志 symbol:method追加 位置:灯泡类型的变量 u
此处的错误是您正在尝试使用不存在的灯泡 class 的 append
方法。相反,我认为您想调用父 class 的 append 方法,例如:
public class UnsortedBulbList extends BulbList{
public UnsortedBulbList(){
}
void add(Bulb u){
super.append(u);
}
}