返回目录:金融新闻
[java] view plain copy
import java.lang.String;
import java.util.*;
publicclass JAVAContainer {
// Interfaces:
// Interface Iterator: hasNext(), next(), remove() ---- 所有容器通用的遍历方式。
// Interface Collection:add(E e),remove(Object o), clear(), isEmpty(), size(), iterator(), toArray() ---- 所有单值容器(map除外)的公共接口。
// Interface Map: put(K key, V value), get(Object key), remove(Object key), clear(),isEmpty(),size(), keySet(), entrySet(), values() ---- 所有K-V容器的公共接口,常见class: HashMap, Hashtable, IdentityHashMap, LinkedHashMap, ConcurrentHashMap
// Interface Map.Entry: getKey(), getValue(), setValue(V value) ---- 用于遍历Map容器。
//
publicstaticvoid main(String[] args) throws Exception {
// ArrayList
{
ArrayList arraylist = new ArrayList();
arraylist.add(0, "end");//指定索引加入值
// 需注意的是,如果现有2个值,我加入索引为5的那么就会出现异常
for (int i = 0; i < 2; i++) {
arraylist.add(i, String.valueOf(i));
}
System.out.println("ArrayList:");
for (int i = 0; i < arraylist.size(); i++) {
System.out.print(arraylist.get(i) + ";");
}
arraylist.add("0");//直接加入值到ArrayList的最后
arraylist.add("0");
System.out.println("");
System.out.print(" ArrayList's lastIndexOf(\\"0\\") is " + arraylist.lastIndexOf("0"));
for (Object val : arraylist) {
System.out.print( val + ";");
}
System.out.println("\\n");
}
// Arrays
{
String[] array = new String[] { "a", "b", "c" };
List list = Arrays.asList(array);
System.out.println(" Arrays:");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + ";");
}
System.out.print(" Arrays's length is " + array.length);//打印数组的长度
System.out.println("");
for (Object val : array) {
System.out.print( val + ";");
}
System.out.println("\\n");
}
// Collections
{
String[] array = new String[] { "a", "b", "c" };
List list = Arrays.asList(array);
Collections.fill(list, "Fill");//用Fill填充全部元素
System.out.println(" Collections:");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + ";");
}
array = new String[] { "1", "2", "3" };
List list2 = Arrays.asList(array);
Collections.copy(list, list2);//拷贝list2的数据进list
System.out.println(" " + list);
Collections.swap(list, 2, 1);//调换索引为1和2的元素的位置
System.out.println(list);
System.out.println("\\n");
}
// EventObject
{
String s = "hello";
String s2 = s;
EventObject eventobject = new EventObject(s);//一个准容器类型,确切的归类它不是容器
System.out.println("EventObject:");
System.out.println(eventobject.getSource());
System.out.println(eventobject.equals(s2));
System.out.println("\\n");
}
// HashMap
{
HashMap hashmap = new HashMap();//一个速度最快的容器
hashmap.put("0", "c");
hashmap.put("1", "a");
hashmap.put("2", "b");
hashmap.put("3", "a");
System.out.println("HashMap:");
System.out.println(hashmap);//该容器有其内部的排序方式
Set set = hashmap.keySet();//获取全部键
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.print(hashmap.get(iterator.next()) + ";");
}
System.out.println("");
System.out.println("HashMap: K-V");
iterator = set.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.print(key+":"+hashmap.get(key) + ";");
}
System.out.println("");
System.out.println("HashMap(Entry): K-V");
Set<Map.Entry> kvSet = hashmap.entrySet();
Iterator<Map.Entry> it = kvSet.iterator();
while (it.hasNext()){
Map.Entry entry = it.next();
System.out.print( entry.getKey()+":" + entry.getValue() +";");
}
System.out.println("\\n");
}
// HashSet
{
HashSet hashset = new HashSet();//一个绝对不能重复的类型
hashset.add("c");
hashset.add("b");
hashset.add("a");
hashset.add("a");
hashset.add("b");
System.out.println(" HashSet:");
System.out.println(hashset);
Iterator iterator = hashset.iterator();//取出元素
while (iterator.hasNext()) {
System.out.print(iterator.next() + ";");
}
System.out.println("\\n");
}
// Hashtable
{
Hashtable hashtable = new Hashtable();//一个完全可以由其他容器替换的老容器类型
hashtable.put("0", "c");
hashtable.put("1", "a");
hashtable.put("3", "c");
hashtable.put("2", "b");
System.out.println(" Hashtable:");
Enumeration enumeration = hashtable.elements();//获取元素,Enumeration已经不是主流,Iterator是它的下一代替代品
while (enumeration.hasMoreElements()) {
System.out.print(enumeration.nextElement() + ";");
}
System.out.println("\\n");
}
// IdentityHashMap
{
IdentityHashMap identityhashmap = new IdentityHashMap();
identityhashmap.put("0", "c");
identityhashmap.put("1", "a");
identityhashmap.put("3", "b");
identityhashmap.put("2", "a");
System.out.println(" IdentityHashMap:");
System.out.println(identityhashmap);
System.out.println(identityhashmap.containsKey("3"));//是否包含这个键
System.out.println(identityhashmap.containsValue("a"));//是否包含值
Set set = identityhashmap.entrySet();//传为Set类型
System.out.println(set);
set = identityhashmap.keySet();//全部键
System.out.println(set);
System.out.println("\\n");
}
// LinkedHashMap
{
LinkedHashMap linkedhashmap = new LinkedHashMap();
linkedhashmap.put("0", "b");
linkedhashmap.put("2", "a");
linkedhashmap.put("1", "c");
linkedhashmap.put("3", "b");
System.out.println("LinkedHashMap:");
System.out.println(linkedhashmap);
System.out.println(linkedhashmap.containsKey("2"));//是否包含这个键
System.out.println(linkedhashmap.containsValue("c"));//是否包含值
Set set = linkedhashmap.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.print(linkedhashmap.get(iterator.next()) + ";");
}
System.out.println("\\n");
}
// LinkedHashSet
{
LinkedHashSet linkedhashset = new LinkedHashSet();//它包含了几种Set的属性但却没有自己的特色
linkedhashset.add("c");
linkedhashset.add("a");
linkedhashset.add("a");
linkedhashset.add("b");
System.out.println(" LinkedHashSet:");
System.out.println(linkedhashset);
System.out.println(linkedhashset.contains("a"));//是否包含对象
Iterator iterator = linkedhashset.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + ";");
}
System.out.println("\\n");
}
// LinkedList
{
LinkedList linkedlist = new LinkedList();//自由使用是它的特色
linkedlist.add("a");
linkedlist.add(1, "c");
linkedlist.addLast("b");
linkedlist.addFirst("d");
System.out.println(" LinkedList:");
System.out.println(linkedlist);
// linkedlist.clear();//该方法清空容器
// linkedlist.remove(0);//删除索引为0的元素
// linkedlist.remove("d");//删除值为d的元素
// linkedlist.removeFirst();//删除第一个元素
// linkedlist.removeLast();//删除最后一个元素
for (int i = 0; i < linkedlist.size(); i++) {
System.out.print(linkedlist.get(i) + ";");
}
System.out.println("\\n");
}
// Stack
{
Stack stack = new Stack();//堆栈
stack.add("b");
stack.add(0, "c");
stack.push("d");
stack.add("e");
stack.push("a");
Enumeration enumeration = stack.elements();
System.out.println(" Stack:");
while (enumeration.hasMoreElements()) {
System.out.print(enumeration.nextElement() + ";");
}
// 后进先出
System.out.println(" " + stack.peek());
System.out.println(stack.pop());
System.out.println(stack.contains("d") + ";" + stack.contains("a"));//是否包含该元素,有趣的事情发生了
System.out.println(stack.search("c"));//非常有用的属性,检索,但是由后向前的排列
System.out.println("\\n");
}
// TreeMap
{
TreeMap treemap = new TreeMap();
treemap.put("0", "d");
treemap.put("2", "a");
treemap.put("1", "b");
treemap.put("3", "c");
System.out.println(" TreeMap:");//可以对键排序
System.out.println(treemap);
System.out.println(treemap.firstKey());//返回第一个键
Set set = treemap.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.print(treemap.get(iterator.next()) + ";");
}
System.out.println("\\n");
}
// TreeSet
{
TreeSet treeset = new TreeSet();//自动排序内容
treeset.add("b");
treeset.add("a");
treeset.add("c");
treeset.add("d");
System.out.println(" TreeSet:");
System.out.println(treeset);
System.out.println(treeset.first());//返回第一个元素
Iterator iterator = treeset.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + ";");
}
System.out.println("\\n");
}
// Vector
{
Vector vector = new Vector();
vector.add(0, "b");
vector.add("a");
vector.addElement("d");
vector.add("c");
System.out.println(" Vector:");
System.out.println(vector);
vector.set(2, "h");//替换掉指定索引的元素
System.out.println(vector);
Object[] str = vector.toArray();
for (int i = 0; i < str.length; i++) {
System.out.print(str[i] + ";");
}
vector.setSize(2);//重新设置大小为2
System.out.println(" " + vector);
System.out.println("\\n");
}
// WeakHashMap
{
WeakHashMap weakhashmap = new WeakHashMap();
weakhashmap.put("1", "b");
weakhashmap.put("2", "c");
weakhashmap.put("0", "d");
weakhashmap.put("3", "a");
System.out.println(" WeakHashMap:");
System.out.println(weakhashmap);
System.out.println(weakhashmap.containsKey("3"));//是否包含键
System.out.println(weakhashmap.containsValue("d"));//是否包含值
Set set = weakhashmap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + ";");
}
// weakhashmap.remove("2");//删除该键对应的值
// weakhashmap.get("1");//获取指定键的值
System.out.println("\\n");
}
}
}
[cpp] view plain copy
import java.io.*;
import java.util.*;
publicclass Main{
publicstaticvoid main(String args[]){
Scanner cin = new Scanner(System.in);
try{cin=new Scanner(new FileInputStream("data.in"));}catch(Exception e){}
int a, b;
while (cin.hasNext()){
a = cin.nextInt(); b = cin.nextInt();
System.out.println(a + b);
}
}
}
作者:张浩斌
链接:https://www.zhihu.com/question/24731236/answer/72682284
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1. 输入
①类形式:
import java.util.*;
②具体方法
读一个整数: int n = sc.nextInt(); 读一个字符串:String s = sc.next(); 读一个浮点数:double t = sc.nextDouble(); 读一整行: String s = sc.nextLine(); 判断是否有下一个输入
2. 输出
①标准输出
System.out.print(); System.out.println(); System.out.format();System.out.printf(); 12341234
②格式化输出
//0指一位数字,#指除0以外的数字(如果是0,则不显示)
3.其他操作
①高精度运算
涉及到两个类BigDecimal(表示浮点数)和BigInteger(表示整数)
使用这两个类的时候需要加上import java.math.*;
(1)valueOf(parament); 将参数转换为指定类型
int a = 1, b =3, c = 10;
②提高读写速度
使用StreamTokenizer 和 PrintWriter
public class Main { public static void main(String[] args) throws IOException
③排序
intArray = new int[] { 4, 1, 3, -23 };
自定义容器排序的两种方法
方法一:让对象类实现comparable接口,覆写compareTo函数,里面用你说的私有数据来比较大小
class xxx implements Comparable{
方法二:新建一个Comparator对象
Comparator comparator=new Comparator<T>() { @Override
④进制转换
String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35).int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt有两个参数,第一个为要转的字符串,第二个为说明是什么进制). BigInter m = new BigInteger(st, base); // st是字符串,base是st的进制.System.out.println("十进制10转16进制为"+Integer.toHexString(10));System.out.println("十进制10转二进制为"+Integer.toBinaryString(10));1234567812345678
⑤四舍五入
System.out.println("2.5的四舍五入值:" + Math.round(12.5));System.out.println("-2.5的四舍五入值:" + Math.round(-12.5));System.out.println("舍掉小数取整:Math.floor(-2.5)=" + (int)Math.floor(-i)); System.out.println("凑整:Math.ceil(-2.5)=" + (int)Math.ceil(-i)); BigDecimal i = num.setScale(2,RoundingMode.HALF_EVEN); 1、 ROUND_UP:远离零方向舍入。向绝对值最大的方向舍入,只要舍弃位非0即进位。2、 ROUND_DOWN:趋向零方向舍入。向绝对值最小的方向输入,所有的位都要舍弃,不存在进位情况。3、 ROUND_CEILING:向正无穷方向舍入。向正最大方向靠拢。若是正数,舍入行为类似于ROUND_UP, 若为负数,舍入行为类似于ROUND_DOWN。Math.round()方法就是使用的此模式。4、 ROUND_FLOOR:向负无穷方向舍入。向负无穷方向靠拢。若是正数,舍入行为类似于ROUND_DOWN;若为负数,舍入行为类似于ROUND_UP。5、 HALF_UP:最近数字舍入(5进)。这是我们最经典的四舍五入。6、 HALF_DOWN:最近数字舍入(5舍)。在这里5是要舍弃的。7、 HAIL_EVEN:银行家舍入法。