List集合特点,遍历方式,ArrayList(去重原理,增长因子论,LinkedList)

for (int i = 0; i < list.size(); i++) {

System.out.println(list.get(i));

}

foreach遍历,代码如下

// 使用foreach遍历

for (String s : list) {

System.out.println(s);

}

迭代器iterater遍历,代码如下

// 使用迭代器遍历(和数据库取数据一样,

Iterator it = list.iterator();

while (it.hasNext()) {// 判断条件

System.out.println(it.next());// 是否有下一个

}

4.LinkedList


实现队列,特点:先进先出,一次只能取一个

实现堆栈,特点:反向输出

代码如下:

public class Demo2 {

public static void main(String[] args) {

LinkedList list=new LinkedList<>();

Dl d=new Dl(list);

d.push(“a”);

d.push(“b”);

d.push(“c”);

/**

  • 队列的特点 先进先出

  • 一次只能取一个

  • 堆栈的特点 反向输出

*/

System.out.println(d.pop());

System.out.println(d.pop());

System.out.println(d.pop());

}

}

//队列

class Dl{

LinkedList ll=null;

public Dl(LinkedList ll){

this.ll=ll;

}

//从容器中取元素

public String pop() {

return ll.removeFirst();//堆栈,removeLast

}

public void push(String s) {

ll.add(s);

}

}

队列输出为abc,堆栈输出为cba

5.增长因子论证


ArrayList集合底层是数组结构,他是“可变数组” 一旦超过默认数组长度,会自动扩容 扩容因子为0.5

代码如下:

public class Demo3 {

public static void main(String[] args) throws SecurityException, Exception{

ArrayList list = new ArrayList<>();

for (int i = 1; i < 101; i++) {

System.out.println(i);

list.add(i);

printgetElementDateLength(list);

}

}

private static void printgetElementDateLength(ArrayList list)throws Exception,SecurityException{

Field f=list.getClass().getDeclaredField(“elementDate”);

f.setAccessible(true);

Object[] elementDate =(Object[]) f.get(list);

System.out.println(“目前list集合底层存储数据的数组长度:”+elementDate.length);

}

}

输出结果为:

6.List集合去重原理


判断是否重复的方法:

List集合特点,遍历方式,ArrayList(去重原理,增长因子论,LinkedList)_第1张图片

去重原理:

当没有写equals方法时,可以加入重复的学生对象,当写了equals方法后,equals方法被调用,不可重复增加

list.add(new Student(“zs”, 16));

list.add(new Student(“ls”, 17));

list.add(new Student(“ad”, 18));

if (!list.contains(new Student(“ad”, 18))) {

list.add(new Student(“ad”, 18));

}

for (Object o : list) {

System.out.println(o);

}

}

}

class Student {

private String name;

private int 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 “Student [name=” + name + “, age=” + age + “]”;

}

public Student(String name, int age) {

super();

this.name = name;

this.age = age;

}

@Override

public int hashCode() {

System.out.println(“hashCode方法被调用—”);

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

System.out.println(“equals方法被调用—”);

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (age != other.age)

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

你可能感兴趣的:(面试辅导大厂内推,list,windows,数据结构)