栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java-01.基础语法-String,HashMap,ArrayList等-switch

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-01.基础语法-String,HashMap,ArrayList等-switch

Byte.MIN_VALUE, Byte.MAX_VALUE
Byte,Short,Integer,Long

Double,Float,Character

boolean类型不能转换

Java switch case 语句 | 菜鸟教程

这里没有说明default在前面的情况

default在switch开头:

若所有case都不满足条件,则执行default,并执行default语句之后的case语句,直到break或结束

【JAVA基础】-- switch case default添加break_杜.的博客-CSDN博客

Number & Math 类方法
Integer x= 5;
return x.doubleValue()

byte/short/long+Value()

Integer x = 5;
return x.compareTo(3);

-1,0,1

Integer x = 5;

x.equals(5); \-----true or false, not null

Integer b = Integer.valueOf("444",16);\字符串或者数字转数字

 x.toString(); \数字转字符串

int b = Integer.parseInt("444",16); \字符串转数字



//Math
Math.abs(-1);
Math.ceil(100.6); // 101
Math.floor(100.6);//100

Math.rint(100.675); // 101.0
Math.round(100.50); // 101, Math.floor(x+0.5)

Math.min(12.2,13.3); // 
//Math.max
// Math.E
// Math.exp(x)
// Math.pow(x,y)
//Math.log(x); ln(x)
// Math.sqrt()
//Math.sin()
//Math.cos()
//Math.tan()
//Math.random()


Java Character 类
Character ch = new Character('a');
// 使用对象
char ch = 'a';
//只能使用Character.method() 不能'a'.method(), ch.method()

Character.isLetter(ch); // true
Character.isDigit(ch);
Character.isWhitespace();

Character.isUpperCase(ch);
Character.isLowerCase(ch);
Character.toUpperCase(ch);
Character.toLowerCase(ch);

Character.toString(ch);



Java String 类
// String 创建的字符串存储在公共池中,而 new 创建的字符串对象在堆上:
String str = "Mike";
String str2=new String("Mike");
String str = "Mike";
char res = str.charAt(1); // 'i'

str.concat(" John");

int index = str.indexof("ke"); // 2 第一次出现或者-1

str.length();

str.split(" ");


str.toCharArray();

str.toLowerCase();

str.contains("ke");

str.isEmpty();

Java compareTo() 方法 | 菜鸟教程

其余的方法刷题中熟悉。

Java StringBuffer 和 StringBuilder 类

一般算法题应该会中规中矩的,应该不至于搞得这么复杂,所以暂时不管这个;

Java 数组
int[] numbers={1,2,3,4};
for(int x: numbers){
System.out.print(x+","); //注意如果是单引号的逗号就会变成相加等于数字了
System.out.format("x = %dn",x)
}
Arrays.binarySearch(sorted_nums, key);
Arrays.equal(nums1,nums2);
Arrays.fill(nums,10); // fill 10
Arrays.fill(nums,1,5,10); // index 1-4, fill 10
Arrays.sort(nums);// only ascending
Java 数据结构

vector

// subclass of vector
Stack stack;
stack.push(1);
stack.peek();
stack.pop();
stack.empty();
stack.search(3); # [1,2,3,4], 从最外面开始搜索,从1 开始,因此是2

Dictionary过时-全都使用Map接口了

hashtable是Dictionary的具体的实现,和hashmap区别在于,他是同步的。已经整合在Map中

Properties 继承于 Hashtable。表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串。

//Properties class inherited from Hashtable
Java 集合框架

collection (List, Set, Queue)and map

接口:是代表集合的抽象数据类型。

  • 实现(类):是集合接口的具体实现。从本质上讲,它们是可重复使用的数据结构,例如:ArrayList、LinkedList、HashSet、HashMap。

Iterator--hasNext, next, remove

Iterator it = nums.iterator();
while(it.hasNext()){
    Integer i = it.next();
    if(i<10){
        it.remove();
    }
}

ArrayList

ArrayList sites = new ArrayList<>();
sites.add("Google");
sites.add("Bing");
sites.get(0);

sites.set(1,"Wiki"); // modify

sites.remove(1);

sites.size(); // size

// size(), get(i)--traverse
// for each
Collections.sort(nums); //sort arraylist

lamda表达式

(int x, int y) -> x + y
// forEach
nums.forEach((e)->{
    e *= 10;
    System.out.println(e);
);

//addAll
nums.addAll(index, Collection c);

nums.clear();

// 可以直接打印
System.out.println(nums);

ArrayList cloneSites = (ArrayList) sites.clone();


nums.contains(1); // 

nums.indexOf("Google"); // the first or -1


// remove

nums.remove(Integer.valueOf(1)); // Object, 此时删除的是元素1
nums.remove(1); // 此时代表int , 删除的是index 1

nums.isEmpty();

nums.subList(1,3);

nums.set(1,3); // index, elements

nums.sort(Comparator.naturalOrder());
nums.sort(Comparator.reverseOrder());

Integer[] numArray = new Integer[nums.size()];
nums.toArray(numArray);

nums.toString();

// 
nums.removeIf(e -> e.contains("Tao")); 

LinkedList

LinkedList list = new LinkedList <>();
list.add(1);
list.add(2);

list.addFirst(3); // 3-->1-->2
list.addLast(4);

list.removeFirst();
list.removeLast();

list.getFirst();
list.getLast(); // O(1), 双向链表
// get不存在的话会报错
list.peekFirst();
list.peekLast();//不存在的话会返回妞,null

list.contains(element);

list.indexOf(Obejct o);

list.poll(); // ==pop



list.add(int index, E element); 

HashSet: 可以有一个null

HashSet set = new HashSet<>();
set.add("Apple");

set.contains("Apple");

set.remove("Pear"); // return false

set.clear();

set.size();

// for each


java创建集合时如(ArrayList, HashMap)等,等号右边 不需要添加泛型的原因_铁汉柔情li的博客-CSDN博客java泛型的理解 - 豆奶特

HashMap, 允许一个null key

collection都是add, map都是put, 有规律的;但是stack是push, pop , peek, empty不同

Map sites = new HashMap<>();

sites.put(1,"Google");
sites.putAll();

sites.get(key); // get value

sites.remove(key); // remove the key-value

sites.clear();

sites.size();


for(Integer i: sites.keySet() ){
    System.out.println("key: " + i + "value: " + sites.get(i));
}

for(String value: sites.values()){
    System.out.print(value+", ");
}



sites.isEmpty();


if(!sites.contains(key)) {
    sites.put(key,value);
}

sites.containsValue(value);



sites.replace(key, newValue); // return true
sites.replace(key,oldValue, newValue); 
// if key-value doesn't exist, return false, no operation
// suitable for replace some special values


java中print()括号中加号(+)的作用_一只攻城狮√的博客-CSDN博客_java输出加号

import java.util.Map.Entry;

for(Entry entry: sites.entrySet()){
    System.out.println("key: "+entry.getKey()+"value: "+entry.getValue());
}

// insert repeated key

prices.merge("shirt",100, (oldValue, newValue) -> oldValue + newValue);
// if key exists, return lambda function, else directly insert the newValue


// compute

int newPrice = prices.compute("Shoes",(key,value)->value-value*10/100);

//getOrDefault, get will return null if not exist, but this will return default

sites.getOrDefault(1,"Not Found");

// 注意clone()只能用于hashmap接口,不能用map接口



浅拷贝只复制指向某个对象的指针,而不复制对象本身,新旧对象还是共享同一块内存, 所以如果其中一个对象改变了这个地址,就会影响到另一个对象。。

浅拷贝对应的就是深拷贝,深拷贝是将一个对象从内存中完整的拷贝一份出来,从堆内存中开辟一个新的区域存放新对象,且修改新对象不会影响原对象。

Java Iterator(迭代器)

Java Iterator(迭代器)不是一个集合,它是一种用于访问集合的方法,可用于迭代 ArrayList 和 HashSet 等集合。

迭代器 it 的两个基本操作是 next 、hasNext 和 remove。

 Iterator it = sites.iterator();
Java Object 类
object.getClass();

obj1.equals(obj2);

Java 泛型

使用 Java 泛型的概念,我们可以写一个泛型方法来对一个对象数组排序。然后,调用该泛型方法来对整型数组、浮点数数组、字符串数组等进行排序。

E-Element, T-Type, K-Key, V-Value, N-Number, ?-undefined

public static  void printArray(E[] inputArray){
    for(E element : inputArray){
        System.out.printf("%s ", element);
       }
       System.out.println();
}

Java 泛型 | 菜鸟教程

Java 实例 

Queue--the interface of LinkedList 

add and remove will output error if failure

//比如有的队列满了,用offer就会返回false

// offer-----poll------peek

Queue queue = new LinkedList();

双端队列(Double Ended Queue),学名Deque

Deque deque = new LinkedList<>();

deque.offerLast("A");
deque.offerFirst("B");

deque.pollFirst();
deque.pollLast();

deque.peekFirst();
deque.peekLast();

deque是一个接口,它的实现类有ArrayDeque和LinkedList。

我们发现LinkedList真是一个全能选手,它即是List,又是Queue,还是Deque。但是我们在使用的时候,总是用特定的接口来引用它,这是因为持有接口说明代码的抽象层次更高,而且接口本身定义的方法代表了特定的用途。

可见面向抽象编程的一个原则就是:尽量持有接口,而不是具体的实现类。

使用Deque - 廖雪峰的官方网站

Java中Comparator的使用https://www.jianshu.com/p/81e5c3e88fc6PriorityQueue

Queue pq= new PriorityQueue<>();

pq.offer(3);
pq.offer(2);
pq.offer(1);
pq.poll(); // 1, the min value

每次弹出最小的元素

使用TreeMap时,放入的Key必须实现Comparable接口。String、Integer这些类已经实现了Comparable接口,因此可以直接作为Key使用。作为Value的对象则没有任何要求。

SortedMap是接口,它的实现类是TreeMap,

Map treeMap = new TreeMap<>();

根据面向抽象编程的原则,应持有Map接口

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035811.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号