Java 如何从Linkedlist类实现方法以反转Linkedlist?
如何使用linkedList类中的方法以相反顺序打印linkedList数组:Java 如何从Linkedlist类实现方法以反转Linkedlist?,java,Java,如何使用linkedList类中的方法以相反顺序打印linkedList数组: for (int j = 0; j < FutureValueLinkedList.size(); j++) { String myArrayLinkedList = FutureValueLinkedList.get(j); System.out.println(myArrayLinkedList + "\t"); } System.out.pr
for (int j = 0; j < FutureValueLinkedList.size(); j++)
{
String myArrayLinkedList = FutureValueLinkedList.get(j);
System.out.println(myArrayLinkedList + "\t");
}
System.out.println();
for(int j=0;j
addLast()能够做到这一点吗
import java.util.*;
import java.text.*;
public class FutureValueApp
{
public static void main(String[] args)
{
LinkedList<String> FutureValueLinkedList = new LinkedList<>();
// display a welcome message
System.out.println("Welcome to the Future Value Calculator");
System.out.println();
// perform 1 or more calculations
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get the input from the user
System.out.println("DATA ENTRY");
double monthlyInvestment = getDoubleWithinRange(sc,
"Enter monthly investment: ", 0, 1000);
double interestRate = getDoubleWithinRange(sc,
"Enter yearly interest rate: ", 0, 30);
int years = getIntWithinRange(sc,
"Enter number of years: ", 0, 100);
// calculate the future value
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
// get the currency and percent formatters
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
// format the result as a single string
String results =
"Monthly investment:\t"
+ currency.format(monthlyInvestment) + "\n"
+ "Yearly interest rate:\t"
+ percent.format(interestRate/100) + "\n"
+ "Number of years:\t"
+ years + "\n"
+ "Future value:\t\t"
+ currency.format(futureValue) + "\n";
// print the results
System.out.println();
System.out.println("FORMATTED RESULTS");
System.out.println(results);
String monthlyInvestmentFormat = currency.format(monthlyInvestment);
String interestRateFormat = percent.format(interestRate/100);
String futureValueFormat = currency.format(futureValue);
FutureValueLinkedList.add(monthlyInvestmentFormat + "\t" +
interestRateFormat + "\t" + Integer.toString(years) +
"\t" + futureValueFormat);
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
System.out.println("Future Value Calculations ");
System.out.println();
System.out.print("Inv/Mo.\tRate\tYears\tFuture Value\n");
for (int j = 0; j < FutureValueLinkedList.size(); j++)
{
String myArrayLinkedList = FutureValueLinkedList.get(j);
System.out.println(myArrayLinkedList + "\t");
}
System.out.println();
}
import java.util.*;
导入java.text.*;
公共类FutureValueApp
{
公共静态void main(字符串[]args)
{
LinkedList FutureValueLinkedList=新LinkedList();
//显示欢迎信息
System.out.println(“欢迎使用未来值计算器”);
System.out.println();
//执行一次或多次计算
扫描仪sc=新的扫描仪(System.in);
字符串选择=“y”;
while(choice.equalsIgnoreCase(“y”))
{
//从用户那里获取输入
System.out.println(“数据输入”);
双月投资=getDoubleWithinRange(sc,
“输入每月投资:”,0,1000);
double interestRate=getDoubleWithinRange(sc,
“输入年利率:”,0,30);
整数年=getIntWithinRange(sc,
“输入年数:”,0,100);
//计算未来价值
双月利率=利率/12/100;
整数个月=年*12;
双倍未来价值=计算未来价值(
月投资,月利息,月);
//获取货币和百分比格式化程序
NumberFormat currency=NumberFormat.getCurrencyInstance();
NumberFormat percent=NumberFormat.getPercentInstance();
百分比。setMinimumFractionDigits(1);
//将结果格式化为单个字符串
字符串结果=
“每月投资:\t”
+currency.format(monthlyInvestment)+“\n”
+“年利率:\t”
+百分比格式(利率/100)+“\n”
+“年数:\t”
+年数+“\n”
+“未来值:\t\t”
+currency.format(futureValue)+“\n”;
//打印结果
System.out.println();
System.out.println(“格式化结果”);
系统输出打印项次(结果);
字符串monthlyInvestmentFormat=currency.format(monthlyInvestment);
字符串interestRateFormat=百分比格式(interestRate/100);
字符串futureValueFormat=currency.format(futureValue);
FutureValueLinkedList.add(monthlyInvestmentFormat+“\t”+
利息格式+“\t”+整数.toString(年)+
“\t”+未来价值格式);
//查看用户是否要继续
系统输出打印(“是否继续?”);
choice=sc.next();
System.out.println();
}
System.out.println(“未来价值计算”);
System.out.println();
System.out.print(“库存/生产任务。\t策略\t耳朵\t未来值\n”);
对于(int j=0;j
如果您只想输出反向列表,可以按照Ostap的建议使用下降迭代器。但是,如果您想将反向列表存储在某个位置,则可以这样做:
LinkedList<String> reversedFutureValueLinkedList = Collections.reverse(FutureValueLinkedList );
LinkedList reversedFutureValueLinkedList=Collections.reverse(FutureValueLinkedList);
看一看
LinkedList<Integer> l = new LinkedList<Integer>();
l.add(1);
l.add(2);
l.add(3);
Iterator i = l.descendingIterator();
while(i.hasNext()) {
System.out.print(i.next() + " ");
}
3 2 1