}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test2 {
/**
* 2、 ArrayList
* 在这个泛型为Integer的ArrayList中存放一个String类型的对象。
*
* @param args
*/
public static void main(String[] args) {
List
String str = "luoyilan";
Method method;
try {
method = list.getClass().getMethod("add", Object.class);
method.invoke(list, str);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(list.get(0));
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test3 {
/**
* 3、 假如我们在开发一个系统时需要对员工进行建模,员工包含 3
* 个属性:姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另为还有一个奖金属性
* 。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。
*
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
//员工类
class PeopleBean {
// 姓名
private String name;
// 工号
private String jobNumber;
// 工资
private float wage;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJobNumber() {
return jobNumber;
}
public void setJobNumber(String jobNumber) {
this.jobNumber = jobNumber;
}
public float getWage() {
return wage;
}
public void setWage(float wage) {
this.wage = wage;
}
}
//经理类
class ManagerBean extends PeopleBean {
// 奖金
private float bonus;
public float getBonus() {
return bonus;
}
public void setBonus(float bonus) {
this.bonus = bonus;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test4 {
/**
* 4、 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...
*
* 把26个字符循环一遍
* 每次循环把字符串中字符遍历一般查找相应字符并统计个数
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//记录在不重复情况下字符个数
int index = 0;
//统计字符窜中每一个字符个数
int length = 0;
int a_ASCII = 65;
boolean boo = false;
String str = "abcdefgf12a35Aaaxyzz";
String[][] arr = new String[26][2];
for (int i = a_ASCII; i < a_ASCII + 32; i++) {
boo = false;
length = 0;
for (int j = 0; j < str.length(); j++) {
String change = ((char) i) + "";
String temp = str.charAt(j) + "";
char test = (char) (temp.charAt(0) - 32);
boolean flag = (change.equals(temp) || change.equals(test+""));
if (flag) {
if (arr[index][0] == null) {
arr[index][0] = temp;
boo = true;
}
length++;
arr[index][1] = length + "";
}
}
if (boo) {
index++;
}
}
for (int i = 0; i < index; i++) {
System.out.print(arr[i][0] + "(" + arr[i][1]+")");
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test5 {
/**
* 5、 有一个类为ClassA,有一个类为ClassB,在ClassB中有一个方法b,此方法抛出异常,在ClassA类中有一个方法a,
* 请在这个方法中调用b,然后抛出异常。在客户端有一个类为TestC,有一个方法为c
* ,请在这个方法中捕捉异常的信息。完成这个例子,请说出java中针对异常的处理机制。
*
* java中针对异常的处理机制: java提供两种处理机制
* (1)用try...catch语句捕获并且处理异常;
* (2)使用throw抛出异常,异常必须是java.lang.Throwable类的对象或者该类的子类的对象;
* (3)使用throws声明方法抛出异常;
* (4)使用finally语句声明在任何情况下都会执行的代码。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestC.c();
}
}
class ClassA {
public static void a() throws ArrayIndexOutOfBoundsException {
// throw new ArrayIndexOutOfBoundsException();
ClassB.b();
}
}
class ClassB {
public static void b() {
throw new ArrayIndexOutOfBoundsException();
}
}
class TestC {
public static void c() {
try {
ClassA.a();
} catch (ArrayIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
System.out.println("捕捉异常的信息Arr");
e.printStackTrace();
} catch (Exception e) {
System.out.println("捕捉异常的信息ex");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test6 {
/**
* 6、 将字符串中进行反转。abcde --> edcba
*
*
* @param args
*/
public static void main(String[] args) {
String str = "abcdef";
//把字符串转换成字符串数组
String[] arr = str.split("");
int len = arr.length;
//根据字符串数组长度求交换次数
int loopNumber = len % 2 == 0 ? len / 2 : len / 2 + 1;
System.out.println(len);
for (int i = 1; i < loopNumber; i++) {
String temp = arr[i];
arr[i] = arr[len - i];
arr[len - i] = temp;
}
for (int i = 0; i < len; i++) {
System.out.print(arr[i]);
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test7 {
/**
* 7、 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:
*
* a: 21 次 b: 15 次 c:: 15 次 把: 7 次 当: 9 次 前: 3 次 ,:30 次
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 读取文本内容
// String str = new
// String(" 把当前文件中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数并输出,例如:");
BufferedReader in = new BufferedReader(new FileReader("D:\\luoyilan.txt"));
String str = null;
String s2 = new String();
while ((s2 = in.readLine()) != null)
str += s2 + "\n";// 如果s2不为空,将s2中的读到的每一行换行后给str
in.close();
int len = 0;
while (str.length() > 0) {
len = str.length(); // 获取第一个字符
String s = str.substring(0, 1); // 用空格替换,以便计算这个字符的个数
str = str.replaceAll(s, ""); // 写入文件,加\r\n换行
System.out.print(s + ":出现" + (len - str.length()) + "次\r\n");
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test8 {
/**
* 8、编写一个程序,获取10个1至20的随机数,要求随机数不能重复。
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 保存生成的数字
int number[] = new int[10];
// 记录生成符合条件的数字个数
int index = 0;
// 记录生成的数字是否符合条件
boolean flag;
while (true) {
flag = true;
int r = new Random().nextInt(20) + 1;
// 遍历数组,判断生成的数是否重复
for (int i = 0; i < index; i++) {
if (r == number[i]) {
flag = false;
break;
}
}
// 如果生成的随机数不重复,则添加到数字中
if (flag) {
number[index++] = r;
}
// 生成的随机数数量足够时跳出循环
if (index == number.length) {
break;
}
}
print(number);
}
public static void print(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test9 {
/**
* 9、
* 在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),
* 否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,
* 应抛出IllegalArgumentException异常
* 。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
*
*
* @param args
*/
public static void main(String[] args) {
String arr[] = { "a", "b", "c", "d", "e", "f", "g" };
int value = -100;
try {
// value = charAt(arr, 'a');
// value = charAt(arr, 'h');
// value = charAt(arr, ' ');
value = charAt(null, 'h');
} catch (IllegalArgumentException e) {
// TODO: handle exception
System.out.println("catch");
e.printStackTrace();
}
System.out.println(value);
}
private static int charAt(String arr[], char c) {
int status = -1;
if (arr == null) {
throw new IllegalArgumentException();
}
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals(c + "")) {
status = i;
break;
}
}
return status;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test10 {
/**
* 10、 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。问:
* 最后剩下的是100人中的第几个人?
*
* @param args
*/
public static void main(String[] args) {
int people[] = new int[100];
// 统计剩余总人数
int count = people.length;
// 统计移除的人数
int removePeople = 0;
// 记录报数人所报数字
int index = 0;
for (int i = 0; i < people.length; i++) {
people[i] = i + 1;
}
// 如果总人数大于1继续报数
while (count > 1) {
index = 0;
for (int i = 0; i < count; i++) {
index++;
if (index == 14) {
people[i] = people[i+1];
count--;
break;
}
if (i + 1 == count) {
i = -1;
}
}
}
for (int i = 0; i < count; i++) {
System.out.print(" " + people[i]);
}
}
}