/**
* 一个 ArrayList 对象 aList 中存有若干个字符串元素,现欲遍历该ArrayList 对象,
* 删除其中所有值为"abc"的字符串元素,请用代码实现
*/
public class Test1 {
public static void main(String[] args){
move_abc();
}
private static void move_abc() {
List aList = new ArrayList<>();
//添加字符串
aList.add("abc");
aList.add("sadw");
aList.add("abc");
aList.add("haga");
aList.add("sjah");
aList.add("dah");
aList.add("abc");
System.out.println("删除前集合" + aList);
//判断元素是否和字符串"abc"相等
aList.removeIf("abc"::equals);
System.out.println("删除后:"+aList);
}
}
/**
* 假如我们在开发一个系统时需要对员工进行建模,员工包含 3 个属性:姓名、工号以及工资。
* 经理也是员工,除了含有员工的属性外,另为还有一个奖金属性。请使用继承的思想设计出员工类和经理类。
*
* 要求类中提供必要的方法进行属性访问。
*/
public class Test2 {
public static void main(String[] args) {
Manager manager = new Manager("那个傻逼", "12122", 3200, 1500);
manager.work();//工作
manager.premiuum();//额外奖金
}
public static class Manager extends Employee {
private double bonus;//经理特有的属性
public Manager(String name, String id, double salary, double bonus) {
super(name, id, salary);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
void premiuum() {
System.out.println(getName() + "经理获得的额外的奖金为:" + bonus);
}
}
/**
* 定义员工类
*/
public static class Employee {
private String name;
private String id;
private double salary;
public Employee(String name, String id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void work() {
System.out.println("员工:" + getName() + ";工号:" + getId() + ";现在的工资是:" + getSalary());
}
}
}
**
* 取出一个字符串中字母出现的次数,如:字符串:"abcdekka27qoq",输出格式为a(2)b(1)k(2)..
* 思路:
* 用Map集合特有的属性,键和值的对应关系
*/
public class Test3 {
public static void main(String[] args) {
System.out.print("请输入你想要统计的字符串:");
Scanner scanner = new Scanner(System.in);
//定义字符串并赋值
String line = scanner.nextLine();//String str = "abcdekka27qoq";
String charCount = charCount(line);
System.out.println(charCount);
}
private static String charCount(String line) {
//new 一个TreeMap集合
Map map = new TreeMap<>();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);//需要判断是否是字母
if ((c >= 'a' && c < 'z') || (c >= 'A' && c < 'Z')) {
Integer integer = map.get(line.charAt(i));
if (integer == null) {
integer = 0;
}
integer++;
map.put(line.charAt(i), integer);
}
}
//统计完之后在处理
StringBuilder buffer = new StringBuilder();
for (Character c : map.keySet()) {
buffer.append(c);
buffer.append("(");
buffer.append(map.get(c));
buffer.append(")");
}
return buffer.toString();
}
}
/**
* 反射:
* 写一个方法,此方法的obj对象中的名为propertyName的属性的值设置为value。
*/
public class Test4 {
public static void main(String[] args) {
String obj = "abc";
String propertyName = "value";
char[] value = {'a'};
setProperty(obj, propertyName, value);
System.out.println(obj);
}
/**
* @param obj :需要设置的对象
* @param propertyName bj的任意属性名
* @param value :需要重置的属性值
*/
private static void setProperty(String obj, String propertyName, char[] value) {
Class extends String> clazz = obj.getClass();
try {
Field field = clazz.getDeclaredField(propertyName);
field.setAccessible(true);
field.set(obj, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 自定义一个properties文件,使用反射的形式加载类
*/
public class Test5 {
public static void main(String[] args) {
invoke_properties();
}
private static void invoke_properties() {
//利用配置文件Properties
Properties props = new Properties();
InputStream in = null;
try {
ClassLoader classLoader = Test5.class.getClassLoader();
in = classLoader.getResourceAsStream("demo.properties");
props.load(in);
String clazzName = props.getProperty("DemoClass");
System.out.println("clazzName = " + clazzName);
//加载类
final Class> clazz = Class.forName(clazzName);
Object instance = clazz.newInstance();
Method method = clazz.getDeclaredMethod("run");
method.setAccessible(true);
method.invoke(instance);
} catch (IOException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 将字符串中进行反转。abcde --> edcba
* 思路:
* 遍历字符串,取出每一个的索引,然后放入一个新字符串中。
*/
public class Test6 {
public static void main(String[] args) {
System.out.print("请你输入你要反转的字符串:");
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
//开始反转
reverse(line);
}
/**
* @param line :需要进行反转的字符串
*/
private static void reverse(String line) {
String newStr="";
for (int i = 0; i < line.length(); i++) {
newStr = line.charAt(i) + newStr;
}
System.out.println(newStr);
}
}
/**
* 使用反射方式调用 setName 方法对名称进行设置,不使用 setAge 方法直接使用反射方式对 age 赋值。
*/
public class Test7 {
public static void main(String[] args) {
System.out.println("即将进行反射赋值:");
useReflect();
}
private static void useReflect() {
try {
Class> aClass = Class.forName("exercise.Person");
Constructor> constructor = aClass.getConstructor(String.class, int.class);
Person person = (Person) constructor.newInstance("那个傻逼", 255);
System.out.println("反射改变前:" + person.toString());
//开始反射
System.out.println("开始反射--------------------------------");
Class extends Person> clazz = person.getClass();
Method setName = clazz.getDeclaredMethod("setName", String.class);
setName.invoke(person, "唐总");
Field field = clazz.getDeclaredField("age");
field.setAccessible(true);
field.set(person, 28);
Class> type = field.getType();
if (type.equals(int.class)) {
int age = field.getInt(person);
System.out.println("年龄:" + age);
}
//修改完成
System.out.println(person.toString());
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | NoSuchFieldException | ClassNotFoundException | InstantiationException e) {
e.printStackTrace();
}
}
}
/**
* 定义一个文件流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5)
*/
public class Test8 {
public static void main(String[] args) {
URL url = Test8.class.getClassLoader().getResource("exercise.txt");
String path = url.getPath();
// File file = new File("exercise.txt");//需要完整的路径
//名/E:/interllij_idea/JavaPoet/out/production/JavaPoet/exercise.txt
File file = new File(path);
readFile(file);
}
private static void readFile(File file) {
//定义一个字节输入流
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
//自定义一个数字大小为5的read缓冲区
byte[] buffer = new byte[5];
int len;
while ((len = fis.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 有 100 个人围成一个圈,从 1 开始报数,报到 14 的这个人就要退出。然后
* 其他人重新开始,从 1 报数,到 14 退出。问:最后剩下的是 100 人中的第几个人?
*/
public class Test10 {
public static void main(String[] args) {
//定义一个可变数量
List circle = new LinkedList<>();//LinkedList增删效率高
for (int i = 0; i < 100; i++) {
circle.add(i);
}
System.out.println();
System.out.println("剩余编号:" + ti(circle, 1, 14));
}
private static Integer ti(List circle, int first, int kill) {
if (circle.size() == 1)
return circle.get(0);
else {
for (int i = 0; i < circle.size(); i++) {
if (first == kill) {//报到14的时候退出
circle.remove(i);
first = 0;
i--;//记得i--,被删除的对象的下一个前移,所以重新计算的位置要从当前位置开始
}
first++;
}
return ti(circle, first, kill);
}
}
}