容器类

Collection

java 删除一个面板 java如何删除一个类_数组


addAll(Collection<? extends E> c) // 继承collection的都有该方法

List

add 添加元素

addAll 添加一个List中所有的元素

contains(e) 是否存在该元素

返回true,若存在

remove(e) 删除元素 boolean

如果e是object,删除第一个出现的e元素,并返回true;如果e是int,删除第一个出现的e元素

并且在List中的元素是int型的时候,List.remove(i),会优先选择remove(int x)

查看java doc里面才发现,里面有两个remove方法,分别定义如下:

public E remove(int index);  
public boolean remove(Object o);


我们传入int类型的时候,会自动被当成上面那个方法来调用。所以,为了移除制定的元素而不至于引起混淆的话,可以将传入的int先封装一下:

col.remove((Integer)10);  
col.remove((Integer)25);

————————————————


Stack
  • isEmpty()
Queue/deque(双向队列)
  • 实现方法 Queue<> q=new LinkedList();
  • offer(E) 添加元素进队列 /offerFirst(E),offerLast(E)
  • poll() 取出队列的元素,并删除此元素 return null if empty / pollFirst(),pollLast()
  • peek() 取出队列的元素 return null if empty /peekFirst(),peekLast()
  • size()
  • isEmpty()

Map

HashMap
  • 遍历
    entrySet():返回Entry<E,E>的集合set,其中Entry是一个内部类,帮助Map的功能实现,其实Map也是一个单项链表,每一个单位就是Entry。

数组

折半查找 binarySearch
  • 如果key在数组中,则返回搜索值的索引;否则返回负数(-插入点)。插入点是索引键将要插入数组的那一个位置(从1开始),即第一个大于该键的元素的索引值+1。
Arrays.binarySearch(Object[] arr ,int start, int end, Object key)
//1.arr要搜索的数组
//2.start和end,非必填, 搜索范围为arr的[start,end]
//3.key是要查询的值
int a[] = new int[] {1, 3, 4, 6, 8, 9};
int x1 = Arrays.binarySearch(a, 5);
int x2 = Arrays.binarySearch(a, 4);
//x1:-4, x2:2
排序

Java中的封装类排序

//lists中的对象String 本身含有compareTo方法,所以可以直接调用sort方法,按自然顺序排序,即升序排序
Collections.sort(lists);

自定义类

//根据上面的方法,在自定义类中实现compareTo方法就可以进行排序
@Override
public int compareTo(A a) {
	return this.order.compareTo(a.getOrder());
}

标准写法

Comparator cmp = new MyComparator();
Arrays.sort(a, cmp);
class MyComparator implements Comparator<Integer>{
  @Override
  public int compare(Integer o1, Integer o2) {
	if(o1 < o2) { 
		return 1;
    }elseif(o1 > o2) {
		return -1;
    }else {
		return0;   
    }
  }    
}
//根据Collections.sort重载方法来实现
Collections.sort(listB, new Comparator<B>() {
	@Override
    public int compare(B b1, B b2) {
	  return b1.getOrder().compareTo(b2.getOrder());
	}
});

简单写法:
Arrays.sort(array,(a,b)->(a-b));

赋值

Arrays.fill(array, 1);

转换成List

Arrays.asList(nodes);// return a List

其他类

随机类

Random r=new Random();
r.nextInt(max);返回 [0,max) 的随机数

String

  • split(String regex, int limit):
  • The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.
  • If the limit n >0 then the pattern will be applied at most n - 1 times, the array’s length will be no greater than n, and the array’s last entry will contain all input beyond the last matched delimiter.
  • If n<0 then the pattern will be applied as many times as possible and the array can have any length.
  • If n=0 then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
String s="jhkh1jhkh1kjh1";
String[] str=s.split("1",20);
//str=["jhkh","jhkh","kjh",""]
String[] str1=s.split("1",-1);
//str1=["jhkh","jhkh","kjh",""]
String[] str2=s.split("1",0);
//str2=["jhkh","jhkh","kjh"]
String[] str3=s.split("1",1);
//str3=["jhkh"]

Scanner

在java中,next()方法是不接收空格的,在接收到有效数据前,所有的空格或者tab键等输入被忽略,若有有效数据,则遇到这些键退出。nextLine()可以接收空格或者tab键,其输入应该以enter键结束。

  • next():
    1、一定要读取到有效字符后才可以结束输入。以空格、回车、Tab键都会视为结束符。
    2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
    3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。next() 不能得到带有空格的字符串。
  • nextLine():
    1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
    2、可以获得空白。
    注意:建议能不使用就尽量不要使用nextLine()。尽量使用next。因为有时会出现吃回车现象
    hasNext()和Next()效果其实是一样的,系统都会等待输入下一个字符,只是返回值不同,hasNext()会返回true,next()返回输入的字符。