题目整理0924
编写一个类Demo,类里面写一个方法method(),对输入的字符串“1,4,7,13,5,17,9”,实现在整形数组中排序输出,并且输出的数组中的数组元素为整数。
package com.oracle.exam4;
import java.util.Arrays;
public class Test4 {
public int[] method(String str){
String[] strs=str.split(",");//拆分实现字符串
int[] ins=new int[strs.length];
for (int i = 0; i < strs.length; i++) {
//ins[i]=Integer.parseInt(strs[i]);//转换数组中元素的类型
if(strs[i].equals("0")){ins[i]=0;}//比较字符串的内容,注意字符串不可用==比较
if (strs[i].equals("1")) {ins[i]=1;}
if (strs[i].equals("2")) {ins[i]=2;}
if (strs[i].equals("3")) {ins[i]=3;}
if (strs[i].equals("4")) {ins[i]=4;}
if (strs[i].equals("5")) {ins[i]=5;}
if (strs[i].equals("6")) {ins[i]=6;}
if (strs[i].equals("7")) {ins[i]=7;}
if (strs[i].equals("8")) {ins[i]=8;}
if (strs[i].equals("9")) {ins[i]=9;}
if (strs[i].equals("10")) {ins[i]=10;}
if (strs[i].equals("11")) {ins[i]=11;}
if (strs[i].equals("12")) {ins[i]=12;}
if (strs[i].equals("13")) {ins[i]=13;}
if (strs[i].equals("14")) {ins[i]=14;}
if (strs[i].equals("15")) {ins[i]=15;}
if (strs[i].equals("16")) {ins[i]=16;}
if (strs[i].equals("17")) {ins[i]=17;}
if (strs[i].equals("18")) {ins[i]=18;}
if (strs[i].equals("19")) {ins[i]=19;}
if (strs[i].equals("20")) {ins[i]=20;}
}
Arrays.sort(ins);//数组排序
return ins;
}
public static void main(String[] args) {
String str="1,4,7,13,5,17,9";
Test4 test=new Test4();
int[] ins=test.method(str);
for (int i = 0; i < ins.length; i++) {
System.out.println(ins[i]+" ");
}
}
}
运行结果

oraclepeixun

浙公网安备 33010602011771号