1.传入一个整型参数n,返回一组不重复的随机数 例如 (n=4 return [1,3,0,2]):
public class RandomArray {
public static int[] RandomArr (int length){
int[] result = new int[length];
List<Integer> list = new ArrayList<Integer>();
for(int i=0; i<length; i++){
list.add(i);
}
for(int j=0; j<length; j++){
result[j] = list.remove(new Random().nextInt(length-j)); //Random.nextInt(n) 返回0到n(不包括n) 随机整数
}
for(int n=0; n<length;n++ ){
if(n==0){
System.out.print("["+result[n]+",");
}else if(n==length-1){
System.out.print(result[n]+"]");
}else{
System.out.print(result[n]+",");
}
}
return result;
}
2.excel解析时,使用数组来记录每一行是否有错误, 有错误就记成1, 无误记成0 , 要求返回有错误的行 : 例如 第1, 5-9, 13-15 行有误
public static String errXs(){
String result="";
int[] m = {0,1,1,0,0,1,1,1,1,1,0,1,1,0,1,0,1,1}; //1, 5-9, ....
int k=0;
int h=0;
for(int i=0; i<m.length;i++){
if(m[i]==1){
if(i>0&&m[i-1]!=1){
k=i;
}
h=i;
if(i==(m.length-1)){
result = result +k+"-"+h;
}
}else if(k!=0&&i<(m.length-1)){
if(k==h){
result = result +k+",";
}else{
result = result +k+"-"+h+",";
}
k=0;
}
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(errXs());
RandomArr(21);
}
}
3. 返回所给字符串 出现次数最多的字符 (测试环境:IE9 控制台)
var words="werwgergdfsgdfgddsdf";
var length;
var word;
var max ={
name:"",
itlength: 0
};
(function(words){ //js (function(){})() 匿名函数的执行
if(!words){
return;
}
length = words.length;
word = words[0];
words = words.replace(new RegExp(word,'g'),'');
length = length- words.length;
if(length> max.itlength){
max ={
name : word,
itlength : length
}
}
arguments.callee(words); // <span style="font-family: Arial, Helvetica, sans-serif;"> arguments.callee(words) 调用当前执行的函数,进行迭代</span>
} )(words);
console.log(max.name +","+max.itlength);