用Java编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。...
时间: 2024-03-22 19:40:55 浏览: 31
好的,以下是一个简单的 Java 类,实现了你的要求:
```java
public class Sorter {
public static Object Sort(Object obj) {
if (obj instanceof String) {
return new StringBuilder((String)obj).reverse().toString();
}
else if (obj instanceof int[]) {
int[] arr = (int[])obj;
Arrays.sort(arr);
return arr;
}
else {
throw new IllegalArgumentException("Unsupported data type");
}
}
}
```
这个类包含了一个 `Sort` 方法,它接收一个参数 `obj`,根据 `obj` 的类型来执行不同的操作。如果 `obj` 是一个字符串,方法会返回反转后的字符串;如果 `obj` 是一个整数数组,方法会返回排序后的整数数组;否则,方法会抛出一个 IllegalArgumentException 异常。
注意:为了方便,这里将方法声明为了静态方法。在实际开发中,你可能需要根据具体情况来决定是否需要使用静态方法。
相关问题
用Java 编写一个类,其中包含一个排序的方法sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出
好的,下面是实现该类的Java代码:
```java
public class SortClass {
public static Object sort(Object data) {
if (data instanceof String) {
return new StringBuilder((String) data).reverse().toString();
} else if (data instanceof int[]) {
Arrays.sort((int[]) data);
return data;
} else {
return null;
}
}
}
```
该类中包含一个sort()方法,接收一个参数data,根据data的类型进行不同的操作。如果data是字符串,则返回反序后的字符串;如果data是整型数组,则对其进行从小到大排序;否则返回null。
示例使用:
```java
public static void main(String[] args) {
// 排序整数数组
int[] arr = new int[]{3, 1, 4, 2};
System.out.println(Arrays.toString((int[]) SortClass.sort(arr))); // [1, 2, 3, 4]
// 反转字符串
String str = "hello world";
System.out.println(SortClass.sort(str)); // "dlrow olleh"
// 其他类型
System.out.println(SortClass.sort(new Object())); // null
}
```
希望能帮到你!
java编写一个类,其中包含一个排序的方法sortO,当传入的是一串整数,就按照从小到大的 顺序输出,如果传入的是一个字符串,就将字符串反序输出。
可以按照以下方式编写一个Java类:
```
public class SortAndReverse {
public static void sortO(int[] arr) {
Arrays.sort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println();
}
public static void sortO(String str) {
String reversedStr = new StringBuilder(str).reverse().toString();
System.out.println(reversedStr);
}
}
```
这个类包含两个静态方法sortO,一个接收整数数组,一个接收字符串。sortO方法对整数数组进行排序,然后按顺序输出。对于字符串,sortO方法会将其反转并输出。
要使用该类,需要在另一个类中实例化SortAndReverse类,并调用sortO方法,如下所示:
```
public class Main {
public static void main(String[] args) {
int[] arr = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
SortAndReverse.sortO(arr);
String str = "Hello, world!";
SortAndReverse.sortO(str);
}
}
```
输出应该是:
```
1 1 2 3 3 4 5 5 5 6 9
!dlrow ,olleH
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)