Java提高

基础:

1、java中的下标只能访问数组,不能访问容器
2、可以不用迭代器遍历,用for
3、大多数方法不返回迭代器,所以不要用迭代器遍历,用for,即java中其实把迭代器淘汰了

ArrayList(数组表)

其对应c++中vector
Long[] a=new Long[v.size()];
a=v.toArray(a);  //转为数组

ArrayList<Integer>v=new ArrayList<>();
v.add(5);v.add(8); //在末尾插入
v.add(1,3);  //在位置1插入3
v.remove(1); //删除位置的元素
v.set(0,1);  //修改
v.indexOf(2); //查找,没找到,返回-1
ArrayList<Integer> v2=new ArrayList<>(); v2=(ArrayList<Integer>)v.clone();  //拷贝
out.print(v.get(0));
v.clear();

Iterator it=v.iterator();    //用迭代器遍历
//Iterator<Integer> it=v.iterator();
while(it.hasNext()) {
    out.print(it.next());
}

Collections.sort(v, (a,b)->(b-a));   //排序


LinkedList(链表)

其对应c++中的list(用法和ArrayList类似)
其实现是用链表实现,而非数组,所以用需要用链表实现的某些数据结构时,也可以用它
即其也可以被用作stack、queue、deque等

HashSet和TreeSet

HashSet是基于哈希表实现的,其无序
TreeSet是基于红黑树实现的,其有序
HashSet<Integer>s=new HashSet<>();
s.add(10);s.add(20);s.add(30);
out.print(s.contains(20)+"\n");
s.remove(20);
Iterator it=s.iterator(); 
//Iterator<Integer> it=s.iterator();
while(it.hasNext()) {    out.print(it.next());
}

//除了以下方法,其他和hashset一样
TreeSet<Integer>s=new TreeSet<>((a,b)->(a-b)); 
s.add(1);s.add(2);s.add(3);
out.print(s.ceiling(2)); //找到>=2的数
out.print(s.higher(2)); //找到>2的数


HashMap和TreeMap

Java中map要转换为set遍历
TreeMap默认升序(key升序)
两个map的用法基本一样,但treemap有序
HashMap<Integer,Integer>mp=new HashMap<>();
mp.put(1,100);
mp.put(2,200);
mp.put(3, 300);
out.print(mp.get(100)+"\n");
mp.remove(1);
for(HashMap.Entry<Integer, Integer> e:mp.entrySet()){     //entry映射项(键-值对)   //用这种遍历方法比较好
	out.print(e.getKey() + " " + e.getValue()+"\n");
}
//System.out.println("kkkk\n");  //系统输入的优先级比out高

Iterator it=mp.keySet().iterator();
while(it.hasNext()) {
    Integer key=(Integer)it.next();
    out.print(mp.get(key)+" ");
}          for (Integer k : mp.keySet()) {
    out.print(mp.get(k)+" ");
}

TreeMap<Integer, String>mp2=new TreeMap<>();
mp2.put(1,"z");mp2.put(2, "y");mp2.put(3, "x");
for(Map.Entry<Integer, String>e:mp2.entrySet()) {
    out.print(e+"\n");
}
Iterator it2=mp2.values().iterator();
while(it2.hasNext()) {
    out.print(it2.next()+" ");
}
for (String s : mp2.values()) {
    out.print(s+" ");
}

队列

ArrayDeque(数组双端队列)
ArrayDeque<Integer>q=new ArrayDeque<>(); //队列
q.offer(1);  //入队   //成功返回true,失败返回false
q.offer(2);
q.offer(3);
out.print(q.peek());  //访问队首
while(!q.isEmpty()) {
    out.print(q.pop());  //访问队首并弹出
}

ArrayDeque<Integer>s=new ArrayDeque<>();
s.push(1);
s.push(2);
s.push(3);
out.print(s.peek());
while(!s.isEmpty()) {
	out.print(s.pop());
}

双端队列

ArrayDeque<Integer>dq=new ArrayDeque<>();
dq.addFirst(2);
dq.addFirst(1);
dq.addLast(3);
out.print(dq.getFirst());
out.print(dq.getLast());
out.print(dq.removeFirst());
out.print(dq.removeLast());

PriorityQueue

对应c++中的priority_queue
它是默认小者优先,从前面出列
PriorityQueue<Integer>pq=new PriorityQueue<>((a,b)->(b-a));
pq.offer(1);   //填加元素
out.print(pq.peek()+"\n");  //访问栈顶
out.print(pq.poll());  //访问栈顶并取出
pq.size();
pq.isEmpty();


字符串

最好还是转为字符数组做(别用字符数组比较)
有删除操作 用 可变字符串
若只有取子串操作 用 字符串
String str="abcdef";
str.charAt(2);

char[] ch=new char[10];
ch=str.toCharArray();  //转数组

str.equals(str2);  //是否相等
str.indexOf(str2); //查找str2,返回第一次出现str2的位置,没有则返回-1
str2=str.substring(2,4);//取子串,从2开始到4-1结束,左闭右开

StringBulider(可变字符串)

java中的String不可改变,所以要修改字符串可用StringBulider(可变字符串)
StringBuilder s=new StringBuilder("123456");
for(int i=0;i<s.length();++i) {  //遍历
    out.print(s.charAt(i));
}
s.append(str);  //在s后面增加str
s.delete(2,3); //删除字符串,左闭右开
s.setCharAt(0, 'a'); //修改
s.indexOf(str2); //find 查找
s.reverse();  //翻转
s.toString();
s.substring(2, 3); //截取,返回的是String类型


注意:
小心double精度问题




全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务