Java 使用递归从数组中获取最大值的对象

Java 使用递归从数组中获取最大值的对象,java,arrays,recursion,methods,Java,Arrays,Recursion,Methods,包类 public class Packet { private int idNumber; public double weight; private String Destination; public Packet(int id, double w, String D) { idNumber = id; weight = w; Destination = D; } public b

包类

public class Packet
{
    private int idNumber;
    public double weight;
    private String Destination;
    public Packet(int id, double w, String D)
    {
         idNumber = id;
         weight = w;
         Destination = D;
    }
    public boolean isHeavy()
    {
        if (weight > 10)
            return true;
        else
            return false;
    }
    public String toString()
    {
         return idNumber + " " + weight + " " + Destination;
    }
    public double getWeight()
    {
        return weight;
    }
    public String getDestination() 
    {
       return Destination;
    }
}
包类

import java.io.*;
public class Packages
{
    public String toString(Packet[] list, int n)
    {

    }
    public void displayHeavyPackages(Packet[] list, int n)
    {

    }
    public void displayPacketsToDest(Packet[] list, int n, String dest)
    {

    }
    public int countPacketsToDest(Packet[] list, int n)
    {

    }
    public Packet maxWeightPacket(Packet[] list, int n)
    {
        if (n > 0)
        {
            return list[n];
        }
        if (list[n].getWeight() > list[n-1].getWeight())
        {
              double pack = list[n-1].getWeight();
              list[n-1].getWeight() = list[n].getWeight();
              list[n].getWeight() = pack;
              maxWeightPacket(list, n-1);
        }
    }
    public double maxWeight()
    {
    }
}
分配

Packet objects have unique ID number, weight in pounds with 2 decimals, and state abbreviation for their destination..  
Class Packet describes one packet and has variables idNumber, weight, and destination of type int, double, and String respectively. In addition, it has the following methods
•   boolean isHeavy () that returns true when packet is above 10 pounds, and false otherwise.
•   String toString()  returns String which is one line string representation of Packet objects.
•   double getWeight() returns packet weight. 
•   String getDestination()returns String which is packets’ destination

Create an input file called "packetData.txt" with the following 7 lines. Each line in the "packetData.txt" file has information about one packet object. 
1001 8.37 CA 
1002 2.17 CT 
1003 11.35 NY 
1004 3.77 MA
1005 9.99 CT 
1006 14.91 VT 
1007 4.97 TX 
1008 14.91 CT
Class Recursion has no variables and has the following methods: All of the methods must be recursive and if they display or return several resulting packets, they should display or return resulting packets in the same relative order as in the list. 

•   String toString(Packet[] list, int n) which returns String representation of entire list of packets with one packet object specified per line.
•   void displayHeavyPackets(Packet[] list, int n)  which displays all heavy packets.
•   void displayPacketsToDest(Packet[] list, int n, String dest)   which displays all packets with the destination state dest. 
•   int countPacketsToDest(Packet[] list, int n, String dest)   which returns the number of packets with the destination dest. 
•   Packet maxWeightPacket(Packet[] list, int n) returns the heaviest packet object. If more than one has the same max weight you can return any one of those packets.
•   double maxWeight () returns the weight (with two decimals) of a heaviest packet object.

Your application should also have class TestPackages with only main method in it, in addition to classes Packet and Recursion..In the main method, create an array packetList that can store up to 100 Packets. Next read data for packets from the input file and assign initial part array of Packets. Also maintain counter variable which will be the number of lines in the input file, and also the number of occupied positions in the array packetList. Next invoke each of recursive methods from class recursion.

PROGRAM RUN outline:
ALL PACKETS
   Display all packets by calling toString() method from class Packages.

All HEAVY PACKETS
    Display all heavy packets

All PACKETS with destination dest
    Display all packets that are shipped to destination state dest. 

The number of packets with destination dest is xxx.

The packet object with max weight is:  X X X X.

The max weight of all packets is XXX.

我试图慢慢地完成这个项目的每一种方法。我正在与递归作斗争,尤其是在maxGetWeight上。我希望如果我能解决这个问题,那么我就可以利用这些知识来解决其他递归方法。

使用递归只是在列表上循环是很奇怪的,但我想这就是有时的赋值

它不是java,但可以使用相同的算法:

让数据包=[ {id:1001,重量:8.37}, {id:1002,重量:2.17}, {id:1003,重量:11.35} ]; console.logheavyestpacketfrompackes; 函数heavyestPacketFrompackets,index,heavyestPacket{ 指数=指数| | 0; heavyestPacket=heavyestPacket | |数据包[索引]| |空; 如果索引>=packets.length返回heavyestPacket; let packet=数据包[索引]; 如果packet.weight>heavyestPacket.weight{ heavyestPacket=数据包; } 返回heavyestpacketfrompackes,索引+1,heavyestPacket;
}您好,我正在努力学习java。您能帮我把这个方法从原来的编程语言翻译成java吗?@user6896528好吧,至少展示一下您的尝试,我会从中提供帮助。真的不应该很难。这里您必须知道的唯一一件可能奇怪的事情是,在JavaScript中,| |运算符是一个虚假的合并,上下文运算符的假设为null。