偶然间做起题来--仅为纪念

java试题1对应test1方法:

请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。

java试题2对应test2方法

编写一个程序,这个程序把一个整数数组中的每个元素用逗号连接成一个字符串,例如,根据内容为[1][2][3]的数组形成内容为"1,2,3"的字符串。


package Frank.practice;



public class Demo1 {
public static void main(String [] args){
String [] array={"abc","cd","es","jio"};
String str="o";
//System.out.println(new Demo1().test1(array, str));
System.out.println(new Demo1().test2(array));
}
public int test1(String [] array,String str) throws IllegalArgumentException{
int ind=-1;
int i=0;
if(array!=null){

for(;i<=array.length-1;i++){
StringBuffer strbuf= new StringBuffer(array[i]);
ind = strbuf.indexOf(str);
if(ind==-1) continue;
else break;
}
}
else throw new IllegalArgumentException();
return i;
}
public String test2(String [] array){
String str="";
for(int i=0;i<=array.length-1;i++){
if(i!=array.length-1)
str=str+new String(array[i])+",";
else 
str=str+new String(array[i]);
}
return str;
}
}

如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。
原文地址:https://www.cnblogs.com/Frank99/p/5400003.html