数组有关的问题
System.arrayCopy()
System.arrayCopy()
有一个数组 int[] a = {1,2,3,4,5},如何删除指定的某个元素 3
int[] a = {1, 2, 3, 2, 5};
int sourcePos = 2;
int deletePos = 1;
int copyLen = a.length-1-deletePos;
System.arraycopy(a,sourcePos,a,deletePos,copyLen);
a[a.length-1] = Integer.MIN_VALUE;
System.out.println(Arrays.toString(a));
数组的帮助类Arrays
排序.sort()
Arrays.sort(a);
System.out.println(Arrays.toString(a));
Queue队列
先进先出 FIFO
offer()
尾部添加
poll()
取得队列第一个元素
package com.ffyc.queue;
import java.util.LinkedList;
import java.util.Stack;
public class MyQueue<T> {
private LinkedList<T> list = new LinkedList<>();
public T pop(){
return list.poll();
}
public void push(T data){
list.offer(data);
}
public boolean isEmpty(){
return list.isEmpty();
}
public int size(){
return list.size();
}
public static void main(String[] args) {
MyQueue<String> q = new MyQueue<>();
q.push("apple");
q.push("banana");
q.push("orange");
q.push("grape");
q.push("kiwi-fruit");
while(!q.isEmpty()){
System.out.println(q.pop());
}
System.out.println("----------------------");
Stack<String> stack = new Stack<>();
stack.push("apple");
stack.push("banana");
stack.push("orange");
stack.push("grape");
stack.push("kiwi-fruit");
while(!stack.isEmpty()){
System.out.println(stack.pop());
}
}
}
Map映射集合
.put(key,value)
remove(key)
.get(key)
在map中存入5本书,使用书ISBN,
Book(isbn, title, author,price,publisher)
Map集合的遍历问题
遍历所有的key
Set<String> set = map.keySet();
for(String key : set){
System.out.println(key+":" + map.get(key));
}
values
Collection<Student> values = map.values();
for(Student s : values){
System.out.println(s);
}
Entry
for (Map.Entry<String, Student> e : map.entrySet()) {
System.out.println(e.getKey()+":" + e.getValue());
}
问题
有个数组,只存有小写字母(a-z),. 统计每个小写字母出现次数?
Set怎么做到不能重复的
{
System.out.println(e.getKey()+":" + e.getValue());
}