gpt4 book ai didi

java - 如何才能更有效地调用多个 ListIterator?

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:18 25 4
gpt4 key购买 nike

所以我的问题是,是否可以通过使用带有列表名称(foodList、foodAmount 等)的数组来简化这段代码,而不是用列表名称(来自所述数组)进行某种循环来替换迭代器中的占位符?

这是代码:

public static void writeFile() throws IOException{
ListIterator<String> foodIterator = GrocerieList.foodList.listIterator(0);
ListIterator<Integer> foodAmountIterator = GrocerieList.foodAmount.listIterator(0);
ListIterator<String> drinkIterator = GrocerieList.drinkList.listIterator(0);
ListIterator<Integer> drinkAmountIterator = GrocerieList.drinkAmount.listIterator(0);

FileWriter writer = new FileWriter("/Users/c-mon/IdeaProjects/final/src/shoppingList.txt");

// FOOD
writer.write("foodList");
writer.write(System.getProperty( "line.separator" ));
if(foodIterator.hasNext()){
do{
writer.write(foodIterator.next());
writer.write(System.getProperty( "line.separator" ));
}while(foodIterator.hasNext());
}else{
writer.write("empty");
writer.write(System.getProperty( "line.separator" ));
}

writer.write("foodAmount");
writer.write(System.getProperty( "line.separator" ));
if(foodAmountIterator.hasNext()){
do{
writer.write(foodAmountIterator.next() + "");
writer.write(System.getProperty( "line.separator" ));
}while(foodAmountIterator.hasNext());
}else{
writer.write("empty");
writer.write(System.getProperty( "line.separator" ));
}

// DRINKS
writer.write("drinkList");
writer.write(System.getProperty( "line.separator" ));
if(drinkIterator.hasNext()){
do{
writer.write(drinkIterator.next());
writer.write(System.getProperty( "line.separator" ));
}while(drinkIterator.hasNext());
}else{
writer.write("empty");
writer.write(System.getProperty( "line.separator" ));
}

writer.write("drinkAmount");
writer.write(System.getProperty( "line.separator" ));
if(drinkAmountIterator.hasNext()){
do{
writer.write(drinkAmountIterator.next() + "");
writer.write(System.getProperty( "line.separator" ));
}while(drinkAmountIterator.hasNext());
}else{
writer.write("empty");
writer.write(System.getProperty( "line.separator" ));
}

writer.close();
}

这是声明列表的地方

import java.util.ArrayList;
import java.util.ListIterator;

public class GrocerieList {

static ArrayList<String> foodList = new ArrayList<>();
static ArrayList<String> hygieneList = new ArrayList<>();
static ArrayList<String> drinkList = new ArrayList<>();
static ArrayList<String> otherList = new ArrayList<>();

static ArrayList<Integer> foodAmount = new ArrayList<>();
static ArrayList<Integer> hygieneAmount = new ArrayList<>();
static ArrayList<Integer> drinkAmount = new ArrayList<>();
static ArrayList<Integer> otherAmount = new ArrayList<>();

public static void foodOutput (){
ListIterator<String> foodListIterator = foodList.listIterator(0);
ListIterator<Integer> foodAmountIterator = foodAmount.listIterator(0);

if(foodListIterator.hasNext()){
System.out.println("\t" + "Food");
do {
System.out.println("\t" + foodAmountIterator.next() + "\t\t" + "\t--\t" + "\t\t" + foodListIterator.next());
}while (foodListIterator.hasNext());
System.out.println(" ");
}else{
System.out.println("\t" + "Food");
System.out.println("\t" + "No food on your list");
System.out.println(" ");
}
}
public static void drinkOutput (){
ListIterator<String> drinkListIterator = drinkList.listIterator(0);
ListIterator<Integer> drinkAmountIterator = drinkAmount.listIterator(0);

if(drinkListIterator.hasNext()){
System.out.println("\t" + "Drinks");
do {
System.out.println("\t" + drinkAmountIterator.next() + "\t\t" + "\t--\t" + "\t\t" + drinkListIterator.next());
}while (drinkListIterator.hasNext());
System.out.println(" ");
}else{
System.out.println("\t" + "Drinks");
System.out.println("\t" + "No drinks on your list");
System.out.println(" ");
}
}
public static void hygieneOutput (){
ListIterator<String> hygieneListIterator = hygieneList.listIterator(0);
ListIterator<Integer> hygieneAmountIterator = hygieneAmount.listIterator(0);

if(hygieneListIterator.hasNext()){
System.out.println("\t" + "Hygiene");
do {
System.out.println("\t" + hygieneAmountIterator.next() + "\t\t" + "\t--\t" + "\t\t" + hygieneListIterator.next());
}while (hygieneListIterator.hasNext());
System.out.println(" ");
}else{
System.out.println("\t" + "Hygiene");
System.out.println("\t" + "No hygiene products on your list");
System.out.println(" ");
}
}
public static void otherOutput (){
ListIterator<String> otherListIterator = otherList.listIterator(0);
ListIterator<Integer> otherAmountIterator = otherAmount.listIterator(0);

if(otherListIterator.hasNext()){
System.out.println("\t" + "Other products");
do {
System.out.println("\t" + otherAmountIterator.next() + "\t\t" + "\t--\t" + "\t\t" + otherListIterator.next());
}while (otherListIterator.hasNext());
System.out.println(" ");
}else{
System.out.println("\t" + "Other Products");
System.out.println("\t" + "No other products on your list");
System.out.println(" ");
}
}

public static void rewe (){
System.out.println("########################################");
System.out.println(" ");
System.out.println("Here is your REWE shopping list!");
System.out.println(" ");
System.out.println("\t" + "Qty" + "\t\t" + "\t--\t" + "\t\t" + "Product");
System.out.println(" ");
foodOutput();
drinkOutput();
hygieneOutput();
otherOutput();
System.out.println(" ");
System.out.println("########################################");
}
public static void lidl (){
System.out.println("########################################");
System.out.println(" ");
System.out.println("Here is your LIDL shopping list!");
System.out.println(" ");
System.out.println("\t" + "Qty" + "\t\t" + "\t--\t" + "\t\t" + "Product");
System.out.println(" ");
drinkOutput();
foodOutput();
otherOutput();
hygieneOutput();
System.out.println(" ");
System.out.println("########################################");
}
public static void edeka (){
System.out.println("########################################");
System.out.println(" ");
System.out.println("Here is your EDEKA shopping list!");
System.out.println(" ");
System.out.println("\t" + "Qty" + "\t\t" + "\t--\t" + "\t\t" + "Product");
System.out.println(" ");
foodOutput();
hygieneOutput();
otherOutput();
drinkOutput();
System.out.println(" ");
System.out.println("########################################");
}
public static void aldi (){
System.out.println("########################################");
System.out.println(" ");
System.out.println("Here is your ALDI shopping list!");
System.out.println(" ");
System.out.println("\t" + "Qty" + "\t\t" + "\t--\t" + "\t\t" + "Product");
System.out.println(" ");
drinkOutput();
foodOutput();
hygieneOutput();
otherOutput();
System.out.println(" ");
System.out.println("########################################");
}
public static void kaufland (){
System.out.println("########################################");
System.out.println(" ");
System.out.println("Here is your KAUFLAND shopping list!");
System.out.println(" ");
System.out.println("\t" + "Qty" + "\t\t" + "\t--\t" + "\t\t" + "Product");
System.out.println(" ");
foodOutput();
otherOutput();
drinkOutput();
hygieneOutput();
System.out.println(" ");
System.out.println("########################################");
}

}

最佳答案

这个怎么样:

private static <T> String arrayListToString(ArrayList<T> list){
if (list.size() == 0){
return "empty\n";
}
StringBuilder sb = new StringBuilder();
for(T t : list){
sb.append(list instanceof ArrayList<String> ? t : Integer.toString(t));
sb.append('\n');
}
}

public static void writeFile() throws IOException{
FileWriter writer = new FileWriter("/Users/c-mon/IdeaProjects/final/src/shoppingList.txt");

writer.write("foodList\n");
writer.write(arrayListToString(GrocerieList.foodList));
writer.write("foodAmount\n");
writer.write(arrayListToString(GrocerieList.foodAmount));
writer.write("drinkList\n");
writer.write(arrayListToString(GrocerieList.drinkList));
writer.write("drinkAmount\n");
writer.write(arrayListToString(GrocerieList.drinkAmount));

writer.close();
}

关于java - 如何才能更有效地调用多个 ListIterator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60176968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com