如何处理自定义链表中的对象?



>我成功地制作了一个链表,但现在我在处理它时遇到了问题。我需要将哪些方法添加到我的 FoodList 类中才能处理我的对象?例如,我需要让用户能够选择手动将食物对象添加到一起,以便我可以打印膳食。另外,我不能使用Java API中的任何集合类。这一切都必须是定制的。

public static void main(String[] args) {
FoodList list = new FoodList();
boolean keepGoing = true;
int scanResultInt;
try
{
//I/O stream
FileReader fr = new FileReader("foodlist.txt");
BufferedReader br = new BufferedReader(fr);
Scanner scan = new Scanner(br);
Food hold = new Food();
while(scan.hasNext()){
list.add(hold = new Food());
String str = scan.next();
//str = scan.next();
hold.setName(str);
str = scan.next();
hold.setGroup(str);
int cal = scan.nextInt();
hold.setNumCal(cal);
double percent = scan.nextDouble();
hold.setPercentDV(percent);
list.add(hold);
}
//System.out.println("" + list.toString());
br.close(); //close I/O stream
}
catch(IOException e){
System.err.println("I/O EXCEPTION: " + e.getMessage());
}
Scanner scan2 = new Scanner(System.in);
do  {
System.out.println("---------------------------------------------------------");
System.out.println("            Welcome to the Parkland Meal Selector"        );
System.out.println("---------------------------------------------------------");
System.out.println("Enter the number of the menu option you would like to select:");
System.out.println("        1) List food database");
System.out.println("        2) Create meal by manual selection");
System.out.println("        3) Create meal by random selection");
System.out.println("        4) Remove foods high in calories");
System.out.println("        5) Exit");
scanResultInt = scan2.nextInt();
switch(scanResultInt) {
case 1: {
System.out.println("" + list.toString());
break;
}
case 2: {
System.out.println("Create-A-Meal Menun");
System.out.println("Enter the name of a food you would like to add:n");
String foodWanted = scan2.next();
/*while( != null){
if(foodWanted.equals());
}*/
/*Food tmp;
for(tmp = head; tmp != null; tmp = tmp.next)
{
result += tmp.f;
}
return result;*/
}
case 3: {
System.out.println("Create meal by random selection: n");
break;
}
case 4: {
System.out.println("Remove Food High In Calories: n");
break;
}
case 5: {
keepGoing = false;
break;
}
}
}
while(keepGoing);
}

这是我的链表:

public class FoodList {
// Class fields
private FoodNode head;
private int listCount;
// Private inner class
private class FoodNode
{
public Food f;
public FoodNode next;
public FoodNode(Food f)
{
this.f = f;
this.next = null;
}
}

// Constructor for LinkedList
public FoodList()
{
// Initialize start of the list
head = null;
listCount = 0;
}
// Add method (adds a reservation to the linked list)
public void add(Food f)
{
// Create a new ReservationNode
FoodNode node = new FoodNode(f);
// If this is the first node
if( head == null )
head = node;
else
{
FoodNode tmp = head;
while(tmp.next != null)
tmp = tmp.next;
tmp.next = node;
}
listCount++
}
/*public boolean hasThatFood(String food){
boolean haveThat = false;
FoodNode tmp;
for(tmp = head; tmp != null; tmp = tmp.next)
{
if (food == f.getName());
haveThat = true;
}
return haveThat;
}*/
/*public boolean hasNext(){
boolean hasNext = false;
if(head != null) {
hasNext = true;
return hasNext;
}
}*/
@Override
public String toString() {
String result = "My Foods:" + 'n';
// Loop through all the reservation nodes
FoodNode tmp;
for(tmp = head; tmp != null; tmp = tmp.next)
{
result += tmp.f;
}
return result;
}
}

和我的食品课

public class Food {
private String name;
private String group;
private int numCal;
private double percentDV;
public Food() {//String name, String group, int numCal, double percentDV
/*this.name = name;
this.group = group;
this.numCal = numCal;
this.percentDV = percentDV;*/
name = "";
group = "";
numCal = 0;
percentDV = 0.0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public int getNumCal() {
return numCal;
}
public void setNumCal(int numCal) {
this.numCal = numCal;
}
public double getPercentDV() {
return percentDV;
}
public void setPercentDV(double percentDV) {
this.percentDV = percentDV;
}
@Override
public String toString() {
return "Food{" +
"name: '" + name + ''' +
", Food Group: '" + group + ''' +
", Calories:  " + numCal +
", Daily Percent: " + percentDV +
'}';
}
}

我知道这是意大利面条代码,但这是我最后的手段。任何帮助将不胜感激!

要对对象进行操作,您必须编写自定义Iterator。我想在这里打开LinkedList源代码并查看它是如何工作的没有什么犯罪行为。

像这样的东西, 你可以在网上找到很多资源,

https://crunchify.com/how-to-implement-a-linkedlist-class-from-scratch-in-java/

这是其一。

public Object getElement(int index)
{
if (index < 0)
return null;
Node Current = null;
if (head != null) {
Current = head.getNext();
for (int i = 0; i < index; i++) {
if (Current.getNext() == null)
return null;
Current = Current.getNext();
}
return Current.getData();
}
return Current;
}

你已经实现了一些复杂的类。它的内在逻辑不像你的问题那样很清楚。因此,几乎任何答案都无法满足您的需求。

如果我愿意,我会尝试推荐使用 java 核心工具的逻辑(不实现类,以最佳方式实现LinkedListArrayList......逻辑应该转换为某种结构性解决方案。例如:

  • 输入点创建并调用stream service以处理提供的输入流;
  • stream handler应该操纵builder;
  • builder结果必须收集到composite;
  • 等等...

如果你以更结构化的方式提供你的逻辑,你会问更明确的问题来指出问题。我也相信你的问题会在这次准备之后消失。


另外,我建议您熟悉下一个GoF模式:builderfactory methodcompositestrategy

相关内容

  • 没有找到相关文章

最新更新