面试知识点

1.自定义对象如何去重
public static void main(String[] args) {

	  List<student> list1 = new ArrayList<student>();
	  list1.add(new student("张三",20));
	  list1.add(new student("李四",15));
	  list1.add(new student("张三",18));
	  list1.add(new student("王五",23));
	  
	    Set<student> set = new HashSet<student>();
	    set.addAll(list1);
	    System.out.println(set);
}

2.自定义对象如何排序
//student类,并实现Comparable接口
class Student implements Comparable{
//姓名,成绩,年龄三个变量
private String name;
private int score;
private int age;

//构造方法
public Student() {}
public Student(String name, int score,int age) {
    this.name = name;
    this.score = score;
    this.age = age;
}

//get set 方法
public String getName() {
    return this.name;
}
public int getScore() {
    return this.score;
}
public int getAge(){
    return this.age;
}
public void setName(String name) {
    this.name = name;
}
public void setScore(int score) {
    this.score = score;
}
public void setAge(int age){
    this.age = age;
}

//重写toString方法
public String toString(){
    return "姓名:"+this.getName()+"\t成绩:"+this.getScore()+"\t年龄:"+this.getAge();
}

@Override
//实现Comparable的compareTo方法
public int compareTo(Student stu) {
    // TODO Auto-generated method stub
    return this.getScore()-stu.getScore();
}

}

public class SortList {
public static void main(String [] args){
//集合的定义
List list = new ArrayList();
//学生对象的加入
list.add(new Student(“张三”,89,20));
list.add(new Student(“李四”,60,21));
list.add(new Student(“路人”,99,15));
//排序
Collections.sort(list);
//遍历输出
for(Student stu : list){
System.out.println(stu.toString());
}
}
}
3.定义一个List集合,集合里面放多个自定义的对象,如何完成去重?
public static void main(String[] args) {
List list1 = new ArrayList();
list1.add(new student(“张三”,20));
list1.add(new student(“李四”,15));
list1.add(new student(“张三”,18));
list1.add(new student(“王五”,23));
Set set = new HashSet();
set.addAll(list1);
System.out.println(set);
}
4.如何创建线程?
第一种方法:继承Thread类
第二种方法:实现Runnable接口
第三种方法:实现callable接口
public class MyThread implements Runnable{

public static void main(String[] args) {
	MyThread t = new MyThread();
	Thread thread = new Thread(t);
	thread.start();
}

@Override
public void run() {
	// TODO Auto-generated method stub
	System.out.println("运行");
}

}
5.反射创建对象,反射获取,属性,方法,泛型
/*

  • 反射:可以在类的运行期间,获取到类的信息(属性 , 方法, 注解, 泛型等等)
  • 反射提高了程序的扩展性()
  • //反射的第一步,都是要获取到Class对象---->>类的信息
  • /
    public class StudentFanShe {
    public void lean(){
    System.out.println(“在学习”);
    }
    public void add(String a){
    System.out.println(“在学习”);
    }
    public int status;
    private String name;
    private int age;
    public StudentFanShe(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    @Override
    public String toString() {
    return “StudentFanShe [name=” + name + “, age=” + age + “]”;
    }
    public StudentFanShe(){
    super();
    }
    /
    • 三种方式获取到Class
    /
    @Test
    public void getClassTest(){
    //1.类名.class student.class
    //2.对象名的getClass
    StudentFanShe studentFanShe = new StudentFanShe();
    Class<? extends StudentFanShe> class1 = studentFanShe.getClass();
    //3.Class.forName();
    String name = studentFanShe.getClass().getName();
    try {
    Class<?> forName = Class.forName(name);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    /
    • JunitTest这个测试工具,要求

    • 1.写的测试方法,访问修饰符必须是public的,返回值必须是void

    • 2.Test写反射的时候,只能有默认的构造方法,如果有多个构造方法就会报,

    • java.lang.IllegalArgumentException: Test class can only have one constructor
      */
      @Test
      public void getClassInfo(){
      //1.
      // Class<? extends Class> class1 = Student.class.getClass();
      StudentFanShe studentFanShe = new StudentFanShe();
      Class<? extends StudentFanShe> class1 = studentFanShe.getClass();
      }
      //获取构造方法
      public static void main(String[] args) {
      //1.
      // Class<? extends Class> class1 = Student.class.getClass();
      StudentFanShe studentFanShe = new StudentFanShe();
      Class<? extends StudentFanShe> class1 = studentFanShe.getClass();
      //获取构造方法
      Constructor<?>[] constructors = class1.getConstructors();
      for (Constructor<?> constructor : constructors) {
      Class<?>[] parameterTypes = constructor.getParameterTypes();
      for (Class<?> class2 : parameterTypes) {
      System.out.println(“constructor parameter =”+parameterTypes);
      }
      }
      //普通方法,调用一个方法
      Method[] methods = class1.getMethods();//利用得到的Class对象的自审,返回方法对象集合
      for (Method method : methods) {

      if ("lean".equals(method.getName())) {
      	StudentFanShe newInstance;
      	try {
      		newInstance = StudentFanShe.class.newInstance();
      		System.out.println("调用lean方法  "+method.invoke(newInstance,null));
      	} catch (Exception e) {
      		// TODO Auto-generated catch block
      		e.printStackTrace();
      	} 
      }
      

      }
      //获取公共的属性 用public修饰的属性
      Field[] fields = class1.getFields();//利用得到的Class对象的自审,返回属性对象集合
      for (Field field : fields) {
      System.out.println(“field for”);
      try {
      StudentFanShe newInstance = StudentFanShe.class.newInstance();
      Class<?> type = field.getType();
      System.out.println("字符的类型 "+type);
      field.set(newInstance, 1);
      System.out.println(“newInstance.status=”+newInstance.status);
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      System.out.println(“public修饰的属性”+field.getName());
      }
      //获取私有的属性[获取到全部的属性包括私有的属性]Field
      Field[] fields1 = class1.getDeclaredFields();
      //获取注解Annotation
      Annotation[] annotations = class1.getAnnotations();
      //获取泛型Generic, 获取父类泛型是,子类必须要new
      Teacher teacher = new Teacher();
      Class<? extends Teacher> class2 = teacher.getClass();
      Class<?> superclass = class2.getSuperclass();
      Type genericSuperclass = class2.getGenericSuperclass();
      System.out.println(“父类泛型”+genericSuperclass);
      System.out.println(“父类”+superclass.getName());
      }
      }
      class Teacher extends A1{
      }
      class A1{
      }
      6.什么是泛型,泛型的作用?
      java 泛型是java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。
      7.什么是树,为什么有红黑树?
      树状图是一种数据结构,它是由n(n>=1)个有限节点组成一个具有层次关系的集合
      红黑树是一种比较宽泛化的平衡树,没AVL的平衡要求高,同时他的插入删除都能在O(lgN)的时间内完成,而且对于其性质的维护,插入至多只需要进行2次旋转就可以完成,对于删除,至多只需要三次就可以完成,所以其统计性能要比AVL树好

8数组结构的特点是什么?
概念:同一种数据类型的集合。
好处:可以自动给数组中的元素从0开始编号,方便操作这些元素。

9.list集合的特点
List 中的元素可以重复,有序,List 有索引(下标)的概念,可以有null元素

10.set集合的特点
Set 中的元素不能重复,无序,Set 没有索引的概念,可以有null元素
对于 Set 表示的集合,通常是遍历操作,没有 get()和 set()方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值