Java 对数组进行递归排序 publicstaticvoidmain(字符串[]args){ BufferedReader br=null; 试一试{ //读取csv文件 br=新的BufferedReader(新的文件阅读器(“/Users/andreacondrut/Desktop/global_management.csv”); //创建用于保存产品的列表 List product2List=new ArrayList(); 字符串行=”; br.readLine(); 而((line=br.readLine())!=null){ 字符串[]productDetails=line.split(逗号分隔符); 如果(productDetails.length>0){ //将product2信息保存到product2对象中 Product2 productList=new Product2(productDetails[0],Double.parseDouble(productDetails[1]), productDetails[2],productDetails[3],Integer.parseInt(productDetails[4]); product2List.add(productList); } 全局库存管理器im=新的全局库存管理器(); 伊姆·索塔雷(产品详情); } //印刷品 for(Product2 e:Product2列表){ System.out.println(e.getName()+”、“+e.getPrice()+”、“+e.getDescription()+”、“+e.getImagePath()”) +“,”+e.getQuantity()); } }捕获(异常ee){ ee.printStackTrace(); }最后{ 试一试{ br.close(); }捕获(IOIE){ System.out.println(“关闭BufferedReader时出错”); 即printStackTrace(); } } void sortArray(字符串[]productDetails){ int n=productDetails.length; 对于(int i=0;i。很抱歉,如果我的代码格式错误,这是我第一次使用堆栈溢出,并且它没有读取顶部的排序方法。如何解决此问题?

Java 对数组进行递归排序 publicstaticvoidmain(字符串[]args){ BufferedReader br=null; 试一试{ //读取csv文件 br=新的BufferedReader(新的文件阅读器(“/Users/andreacondrut/Desktop/global_management.csv”); //创建用于保存产品的列表 List product2List=new ArrayList(); 字符串行=”; br.readLine(); 而((line=br.readLine())!=null){ 字符串[]productDetails=line.split(逗号分隔符); 如果(productDetails.length>0){ //将product2信息保存到product2对象中 Product2 productList=new Product2(productDetails[0],Double.parseDouble(productDetails[1]), productDetails[2],productDetails[3],Integer.parseInt(productDetails[4]); product2List.add(productList); } 全局库存管理器im=新的全局库存管理器(); 伊姆·索塔雷(产品详情); } //印刷品 for(Product2 e:Product2列表){ System.out.println(e.getName()+”、“+e.getPrice()+”、“+e.getDescription()+”、“+e.getImagePath()”) +“,”+e.getQuantity()); } }捕获(异常ee){ ee.printStackTrace(); }最后{ 试一试{ br.close(); }捕获(IOIE){ System.out.println(“关闭BufferedReader时出错”); 即printStackTrace(); } } void sortArray(字符串[]productDetails){ int n=productDetails.length; 对于(int i=0;i。很抱歉,如果我的代码格式错误,这是我第一次使用堆栈溢出,并且它没有读取顶部的排序方法。如何解决此问题?,java,recursion,Java,Recursion,我正在尝试按数量和名称对产品数组进行递归排序。我远未成功。不幸的是,我发现的任何示例我都不理解。此代码给了我一个错误,上面写着“未解决的编译问题: 未为参数类型java.lang.String、java.lang.String定义运算符>。很抱歉,如果我的代码格式错误,这是我第一次使用堆栈溢出,并且它没有读取顶部的排序方法。如何解决此问题?我建议您做一些改进/更改: 将productDetails[j]>productDetails[j+1]更改为productDetails[j].compar

我正在尝试按数量和名称对产品数组进行递归排序。我远未成功。不幸的是,我发现的任何示例我都不理解。此代码给了我一个错误,上面写着“未解决的编译问题:
未为参数类型java.lang.String、java.lang.String定义运算符>。很抱歉,如果我的代码格式错误,这是我第一次使用堆栈溢出,并且它没有读取顶部的排序方法。如何解决此问题?

我建议您做一些改进/更改:

  • productDetails[j]>productDetails[j+1]
    更改为
    productDetails[j].compareTo(productDetails[j+1])>0
    在java中是
    Comparable
    。您已经为字符串对象定义了一个compareTo。有关其工作方式的详细信息,请查看
  • 在设计方面,为什么不使用已由提供给您的排序
  • 您正在使用类似的东西。它的时间复杂度为
    O(n^2)
    。为什么不使用某种排序算法,该算法采用
    O(n*log(n))
    ?更好的建议是使用时间复杂度为
    O(n*log(n))
  • 避免将所有内容放在
    main
    方法中。模块化您的代码
  • 避免将整个方法包装在
    try catch
    中。更好的做法是在方法签名中添加
    throws

  • 虽然这个答案评论了很多你没有问过的问题,但我认为你的意图是学习和改进,我只是在这方面给你一点帮助:)

    有什么理由不使用标准库功能吗?如果没有,请看一看。你定义了
    String[]productDetails
    并试图使用“>”该数组的两个元素
    productDetails[j]>productDetails[j+1]
    上的运算符是字符串。这是不允许的。
    String
    可比的
    ;Java中没有运算符重载(因此,您不能使用
    来比较两个字符串。请查看String类中的内容。如其他注释所述,您不能像在C++中那样在Java中重载运算符,最好使用而不是原始字符串。)
    
    public static void main(String[] args) {
    
        BufferedReader br = null;
        try {
            // reading csv file
            br = new BufferedReader(new FileReader("/Users/andreeacondrut/Desktop/global_management.csv"));
    
            // create list to hold products
    
            List<Product2> product2List = new ArrayList<Product2>();
    
            String line = "";
    
            br.readLine();
    
            while ((line = br.readLine()) != null) {
                String[] productDetails = line.split(COMMA_DELIMITER);
    
                if (productDetails.length > 0) {
                    // save the product2 information into product2 object
                    Product2 productList = new Product2(productDetails[0], Double.parseDouble(productDetails[1]),
                            productDetails[2], productDetails[3], Integer.parseInt(productDetails[4]));
    
                    product2List.add(productList);
    
                }
                Global_Inventory_Manager im = new Global_Inventory_Manager();
                im.sortArray(productDetails);
            }
    
            // print product
            for (Product2 e : product2List) {
                System.out.println(e.getName() + "," + e.getPrice() + "," + e.getDescription() + "," + e.getImagePath()
                        + "," + e.getQuantity());
            }
    
        } catch (Exception ee) {
            ee.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException ie) {
                System.out.println("ERROR occured while closing the BufferedReader.");
                ie.printStackTrace();
            }
        }
        void sortArray(String[] productDetails) {
        int n = productDetails.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1; j++) {
                if (productDetails[j] > productDetails[j + 1]) {
                    String temp = productDetails[j];
                    productDetails[j] = productDetails[j + 1];
                    productDetails[j + 1] = temp;
                }
            }
        }
    }