请将Simple类由非泛型类改为泛型类,并用int数组、float数组和string数组进行测试。
时间: 2024-05-14 13:13:12 浏览: 20
好的,将Simple类改为泛型类后,可以用以下代码进行int数组、float数组和string数组的测试:
```
public class Simple<T> {
private T[] arr;
public Simple(T[] arr) {
this.arr = arr;
}
public void print() {
for (T t : arr) {
System.out.print(t + " ");
}
System.out.println();
}
}
```
测试代码如下:
```
public static void main(String[] args) {
Integer[] intArr = {1, 2, 3};
Simple<Integer> intSimple = new Simple<>(intArr);
intSimple.print();
Float[] floatArr = {1.1f, 2.2f, 3.3f};
Simple<Float> floatSimple = new Simple<>(floatArr);
floatSimple.print();
String[] strArr = {"Hello", "World", "!"};
Simple<String> strSimple = new Simple<>(strArr);
strSimple.print();
}
```
输出结果:
```
1 2 3
1.1 2.2 3.3
Hello World !
```
希望能够解决您的问题!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)