public class MyTest {
public String getString(int num) {
String s = "(ABC(DE)FG(HI(JK)LMNO(PQ)RS(T(UV))W))";
char[] c = s.toCharArray();
int left = 1;// 左括号的个数
int right = 0;// 右括号的个数
int numIndex = 0;// 所求的第几对括号中左括号的位置
int index = 0;// 所求的第几对括号中右括号的位置
// 该循环可以得到所求的第几对括号中左括号的位置
for (int i = 0; i < c.length; i++) {
if ('(' == c[i]) {
numIndex++;
}
if (numIndex == num) {
numIndex = i;
break;
}
}
// 该循环可以得到所求的第几对括号中右括号的位置
for (int i = numIndex + 1; i < c.length; i++) {
if (')' == c[i]) {
right++;
}
if ('(' == c[i]) {
left++;
}
if (right == left) {
index = i;
break;
}
}
String newString = s.substring(numIndex + 1, index);
return newString;
}
public static void main(String args[]) {
MyTest test = new MyTest();
System.out.println(test.getString(6));
}
}
1、获取第n对括号中的内容,应该不是最简单的,但可以勉强实现(*^__^*),上面为实现方法。
2、求Math.round(11.5)=? 和Math.round(-11.5)=? 和Math.floor(11.5)=? 和Math.floor(-11.5)=? 。
答案:12;-11;12.0;-12.0。
3、两个整形变量互换值 不用第三个变量传递怎么实现。
a=a+b;
b=a-b;
a=a-b;
4、
import java.util.ArrayList;
import java.util.List;
public class A {
public static void main(String[] args) throws Exception {
String s = "111";
List lst = new ArrayList();
lst.add("aaa");
A.zzz(lst, s);
for (Object o : lst) {
System.out.print(o);
}
System.out.println(s);
}
private static void zzz(List lst, String s) {
lst.add("bbb");
s = "222";
lst = new ArrayList();
lst.add("ccc");
}
}
输出为aaabbb111