List of common interview questions and a simple set of ideas

Here Insert Picture Description

1, a frame set (Introduction, Collection method, iterator)

Collection container is quite specific methods Iterator; crane machines corresponding to the clip, the tool elements of the container taken out

"Interview questions a. Remove methods remove methods for the collection and iterator What is the difference?
1. When an iterator or foreach loop to remove what prone to frequently asked questions (FAQ !!!)
will subscript bounds exception
Here Insert Picture Description
2. In the iterator will perform collection.remove method to pay attention to what the problem util.concurrentModificationException
come from nature He said: concurrency issues ; (crane machines as an example)
Code:

package com.wxm; 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
 /** * collection中特别的方法 * iterator迭代器 *  *  *  * 集合的remove和迭代器的remove有什么区别 * @author wxm * */
 public class CollectionDemo {
 public static void main(String[] args) {
 	Collection c= new ArrayList();
 		c.add(22);	
 		c.add(23);	
 		c.add(34);
 	         c.add(35);	
 	         c.add(48);	//	
 	         //fore//	
 	         for (Object object : c) {4//		
 	         System.out.println(object);
 	         //	}	//迭代器是集合的特有方法  it.next()下一个	
 	         Iterator it=c.iterator();	
 	         while(it.hasNext()) {	
 	         //System.out.println(it.next());		
 	         int num=(int) it.next();		
 	         if(num%2==0) {		
 	          System.out.println(num);								     //System.out.println(it.next());		
 	          }
 	          	}
 	          	}	
 	          			}

Here Insert Picture Description
Here Insert Picture Description

2, the set of frame List (ArrayList specific methods, specific iterator, the object-specific characteristics, demonstrated a growth factor)

Such containers can be seen that there is a subscript, can pick up according to the following standard, and the like ... way to delete the operation of the elements of the container
"face two questions. Linkedlist with the difference 2.1arraylist (CRUD aspect, structure)
Arraylist array structure deletions slow, fast continuous query index threads are not synchronized growth factor of 1.5 10 Linkedlist additions and deletions to the list structure fast, slow queries no continuous subscript
Here Insert Picture Description

```
package com.wxm; import java.util.ArrayList;
import java.util.Collection;import java.util.List;import java.util.ListIterator; 
/* * list中的listIterator *  *  * */
public class ListDemo {
public static void main(String[] args) {
	List c= new ArrayList();
	c.add(22);	
	c.add(23);	
	c.add(34);	
	c.add(35);	
	c.add(48);	
	ListIterator  it=c.listIterator();	
	while(it.hasPrevious()) {//	
		System.out.println(it.previous());		
			}
				System.out.println("---");
				}
				}~
```

Here Insert Picture Description
2.2 arraylist list and the difference between
the length of the variable 1.list, fixed length array
2.list can store all kinds of element object, and an array of a statement can only store a corresponding type,
2.3arraylist how to tune (growth factor)
Why list set underlying structure is an array, but the array length and fixed, and how to change the length of the list? (Argument) ~

package com.wxm; import java.lang.reflect.Field;import java.util.ArrayList;
 /** * 面试题: * arraylist与array的区别 *  
 1.list长度可变,数组长度固定、
 2.list可以存放各类元素对象,对象一旦声明类型,就不能便 * 
  * arraylist如何调优 * 增长因子,来实现优化性能 * 
  * @author wxm * */
  * public class ListDemo2 {
  * public static void main(String[] args) {
  * 	ArrayList al=new ArrayList<>();	
  *     for(int i=1;i<=80;i++) {		
  *            al.add(i);	
  *    	System.out.println(i+",");		
  *     getlength(al);	
  * }		
  * } 
  *  public static void getlength(ArrayList al) {	
  * try {	
  * 	Field f=al.getClass().getDeclaredField("elementData");	
  * 	f.setAccessible(true);		
  *       Object obj=f.get(al);		 
  * Object [] elementData=(Object[]) obj;		
  * System.out.println("当前al容器的底层数组的长度:"+elementData.length);					} catch (Exception e) {		
  * e.printStackTrace();	
  * }
  * }}~

Here Insert Picture Description

3, the frame set LinkedList

Analog queue linked list data structure and Stack: bullet clip last-out queue: FIFO pipe

package com.wxm; import java.util.Iterator;import java.util.LinkedList; 
/** * 面试题:
 * 通过linkedlist集合来制作一个堆栈结构的容器 *  *  
* 模拟一个队列结构的容器 */
*public class LinkedListDemo {   
  public static void main(String[] args) {	 
   Duilie dl=new Duilie();	
     dl.push("a");	
    dl.push("b");	
    dl.push("c");	
    dl.push("c");	
    dl.push("d");	 
    dl.push("e");	  	 
     dl.bianLi(); 
       }
     }
     	/*	 * 堆栈结构的容器	 * */
     		class Duilie{	
     			private LinkedList ll=new LinkedList<>();		
     			/*		 * 往堆栈容器添加元素		 * 		 * */	
     				                  public void push (Object obj) {		
     					ll.addLast(obj);	
     					}			
                                             	  public Object pop() {      
                                             	          return ll.removeFirst();	
     					}						
     					public void bianLi() {//	
     						for (int i = 0; i < ll.size(); i++) {//	
     							System.out.println(this.pop());//		}						Iterator it=ll.iterator();			
     					while(it.hasNext()) {				
     					System.out.println(it.next());			
     					}			
     						}			
     							}~

Here Insert Picture Description
Here Insert Picture Description

5, the set of frame ArrayList repeating elements in its underlying principle to heavy

Analyzing the set of list element are the same, the method is based on the element equals ~
string to weight list stored in the string, the string is the string equals method than the reference value deduplication data type (the name of the same view of the same age the same person)
ideas -

1, description of the person, the data is encapsulated into the object 2, object 3 into the container, to re-reference data type is called a verification method equals ~

package com.wxm; import java.util.ArrayList; 
/** *  * 对arrayList中的元素去重 
1.元素为字符串 2.元素是自定义对象 *  *  
*
需求:
 *  判定两个人是同一个人的依据; 
*  名字相同年龄相同 * 
@author wxm
 *集合collection的contains在调用的时候底层调用容器元素对象的equals方法 * *之前元素对象是String *元素对象是obj(person) */
 public class ArrayListDemo {
 public static void main(String[] args) {
 	ArrayList al=new ArrayList<>();//	
 	al.add("wangting");//	
 	al.add("zhuangyuan");
 	//	al.add("xiang");
 	//	al.add("runchen");
 	//	al.add("xiang");		
 	System.out.println("无去重复"+al.size());
 	//	ArrayList newal=	repeat(al);
 
//	System.out.println("去重复后"+newal.size());	
al.add(new Person("wangting5", 12));	
al.add(new Person("wangting1", 12));	
al.add(new Person("wangting2", 12));    
al.add(new Person("wangting3", 12));	
al.add(new Person("wangting1", 12));	
System.out.println(al.size());
}
	/**	 * 	 * ArrayList 怎么去重复	 * 	
	 * 思路:	
	 * 1.建立一个新容器,	 * 
	2。将老的容器遍历取出其中的元素	 * 
	3.如果这个元素存在于新容器中,那么不再往新容器加入。如果不存在,就加	 */		
	public static ArrayList repeat(ArrayList al) {	
		ArrayList newAl =new ArrayList<>();	
			for(Object obj:al) {			
			   if(!newAl.contains(obj)) {			
			   	newAl.add(obj);		
			   		}	
			   			}		
			   		return newAl;	
			   		}							
			   		}
			  cclass Person{
			   			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 "Person [name=" + name + ", age=" + age + "]";	}	
			   						public Person(String name, int age) {		
			   						super();	
			   						this.name = name;	
			   							this.age = age;	} 
			   							public Person() {		super();	}				
			   						@Override	
			   					public boolean equals(Object obj) {
			   						Person p=(Person) obj;	
			   						System.out.println(p.name+"--equals--"+this.name);	
			   							return p.name.equals(this.name) && p.age==this.age;	}}~

Guess you like

Origin blog.csdn.net/weixin_44106334/article/details/94652783