所以我现在正在制作代码,搜索arraylists并打印出最大的代码。此代码提示用户输入BankAccount名称以及帐户有多少钱。目前我遇到的问题是不知道如何通过arraylists搜索最大数字。我也不知道如何将每个余额与maxBalance进行比较。感谢任何帮助,谢谢。
import java.io.*;
import java.util.*;
import java.text.*;
public class Project43Tester
{
public static void main(String args[])
{
NumberFormat formatter = NumberFormat.getNumberInstance( );
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String name;
ArrayList <String> aryLst = new ArrayList<String>();
do
{
Scanner kbReader = new Scanner(System.in);
System.out.print("Please enter the name to whom the account belongs.(\"Exit\" to abort)");
name = kbReader.nextLine( );
if( !name.equalsIgnoreCase("EXIT") )
{
System.out.print("Please enter the amount of the deposit. ");
double amount = kbReader.nextDouble();
System.out.println(" ");
ArrayList <String> BankAccount = new <String> ArrayList();
AryLst.add(BankAccount);
}
}while(!name.equalsIgnoreCase("EXIT"));
Search aryList and print out the name and amount of the largest bank account
BankAccount ba = //get first account in the list
double maxBalance = ba.balance;
String maxName = ba.name;
for(int j = 1; j < aryLst.size( ); j++)
{
?
? Step through the remaining objects and decide which one has
largest balance (compare each balance to maxBalance)
?
}
System.out.println(" ");
System.out.println("The account with the largest balance belongs to " + maxName + ".");
System.out.println("The amount that the account contains is $" + formatter.format(maxBalance) + ".");
}
}
答案 0 :(得分:1)
您必须遍历ArrayList。在遍历ArrayList期间,将第一个元素设置为max并将其与next进行比较。如果它更大,则将新元素设置为最大值
答案 1 :(得分:1)
最简单的方法是使用ArrayList's sort function.
没有头痛。
请参阅此link
答案 2 :(得分:0)
List<String> l = new ArrayList<String>();
l.add("2.33");
l.add("3.45");
l.add("1.11");
Collections.sort(l);
for(String s: l) {
System.out.println(s);
}
结果:
1.11
2.33
3.45
答案 3 :(得分:0)
这是您的代码的略微修改版本。建议使用Comparable / Comparator进行排序(您应该尝试;修改下面的代码)。
这是通过数组迭代和访问示例的对象属性的简单遍历。
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Project43Tester
{
static class BankAccount
{
String name;
Double amount;
public BankAccount(String name, Double amount)
{
this.name = name;
this.amount = amount;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Double getAmount()
{
return amount;
}
public void setAmount(Double amount)
{
this.amount = amount;
}
}
public static void main(String args[])
{
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String name = null;
List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
do
{
Scanner kbReader = new Scanner(System.in);
System.out.print("Please enter the name to whom the account belongs.(\"Exit\" to abort)");
name = kbReader.nextLine();
if (!"EXIT".equalsIgnoreCase(name))
{
System.out.print("Please enter the amount of the deposit. ");
double amount = kbReader.nextDouble();
System.out.println(" ");
bankAccounts.add(new BankAccount(name, amount));
}
}
while (!"EXIT".equalsIgnoreCase(name));
BankAccount maxBankAccount = null;
for (BankAccount bankAccount : bankAccounts)
{
if (maxBankAccount == null)
{
maxBankAccount = bankAccount;
}
else if(bankAccount.getAmount() > maxBankAccount.getAmount())
{
maxBankAccount = bankAccount;
}
}
System.out.println(" ");
System.out.println("The account with the largest balance belongs to " + maxBankAccount.getName() + ".");
System.out.println("The amount that the account contains is $" + formatter.format(maxBankAccount.getAmount()) + ".");
}
}