简体   繁体   English

如何在不使用导入语句的情况下从 Arrays class 调用方法?

[英]How to call a method from the Arrays class without using an import statement?

I tried to call this method from the Arrays class without using an import statement: Could u please tell me what´s wrong?我尝试在不使用导入语句的情况下从 Arrays class 调用此方法:您能告诉我有什么问题吗?

//import java.util.Scanner;

class ArraySorting {
    /**
     * @param array unordered sequence of strings
     * @return ordered array of strings
     */
    public static String[] sortArray(String[] array) {
        // write your code here
        java.util.Arrays.sort  input = new  java.util.Arrays.sort(System.in);
        String string = input.next();
        String[] str = string.split(" ");
        java.util.Arrays.sort (array);
        System.out.println(string.toString(array));
    }
      return (array);
    }
    public static void main(String[] args) {
        sortArray();
    }
}

Thanks alot非常感谢

The method to sort the array should be cleaned out of the redundant code and has to be as simple as:对数组进行排序的方法应该从冗余代码中清除,并且必须像这样简单:

public static String[] sortArray(String[] array) {
    java.util.Arrays.sort (array);
    System.out.println(java.util.Arrays.toString(array));
    return array;
}

Next, the test must be rewritten to read a line, split into arrays of strings to be sorted with sortArray :接下来,必须重写测试以读取一行,将要使用 sortArray 排序的字符串拆分为sortArray

public static void main(String ... args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    String string = input.nextLine(); // read entire line not a single token
    String[] str = string.split(" ");
    System.out.println("before sort: " + java.util.Arrays.toString(str));
    String[] sorted = sortArray(str);
    System.out.println("str: " + java.util.Arrays.toString(str));
    System.out.println("sorted: " + java.util.Arrays.toString(str));
}

Online demo在线演示

before sort: [abc, aad, 0123, 456, zxy, aabc]
[0123, 456, aabc, aad, abc, zxy]
str: [0123, 456, aabc, aad, abc, zxy]
sorted: [0123, 456, aabc, aad, abc, zxy]

Note: Arrays.sort modifies its input array so actually method sortArray may be void .注意: Arrays.sort修改了它的输入数组,所以实际上方法sortArray可能是void

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在打印语句中从不同类调用方法? - How to call method from different class within a print statement? java中如何在没有main方法的情况下从一个类调用toString()方法到另一个类 - How to call toString() Method from one class to another class without main method in java 如何在不创建子类和/或继承类的情况下从另一个类调用Object的方法? - How to call an Object's Method from another class without creating a sub-class and/or inheriting class? 如何在不知道类名的情况下调用方法? - how to call method without knowing the class name? 如何从另一个 class 调用方法,该方法具有准备在同一个 class 中调用两个方法的 switch 语句? - How do you call a method from another class which has a switch statement ready to call two methods in the same class? 如何使用classLoader从加载的类中调用方法? - How to call a method from loaded class using classLoader? 如何从不同的类调用参数化方法(使用dataprovider)? - How to call a parameterized method (using dataprovider) from a different class? 如何从使用类加载器创建的类中调用方法 - How to call a method from class created using classloader 不使用子类对象调用抽象超类的方法 - Call method of abstract super class without using sub class object 从另一个包中调用类的方法而不实例化该类 - Call method of class from another package without instantiating the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM