java语法
参考: https://www.kancloud.cn/fruitbag/thealgorithm/275925 https://www.runoob.com/java/java-tutorial.html 概念
多态、继承、封装、抽象、类、对象、实例、方法、重载
byte 数据类型是8位、short 数据类型是 16 位、int 数据类型是32位、long 数据类型是 64 位、float 数据类型是单精度32位、double 数据类型是双精度64 位、boolean数据类型表示一位的信息;、har类型是一个单一的 16 位 Unicode 字符;
string
String
[] str
= new String[]{"abc","123","78"};
char[] helloArray
= { 'r', 'u', 'n', 'o', 'o', 'b'};
String str
= new String(helloArray
);
String str
= new String("abcedfghig");
String
[] arr
= str
.split("");
char[] chr
= s2strtoCharArray();
char ch
= s2
.charAt(4);
int index
= s2
.indexOf('r');
String s1
= new String();
String s2
= "billryan";
int s2Len
= s2
.length();
s2
.substring(4, 8);
StringBuilder s3
= new StringBuilder(s2
.substring(4, 8));
s3
.append("bill");
String s2New
= s3
.toString();
StringBuffer 与 StringBuilder
, 前者保证线程安全,后者不是,但单线程下效率高一些,一般使用StringBuilder
char[] chr
= new char[]{'a','b','c'};
chr
.toString();
String
.valueOf(chr
);
四、处理末位带空格的情况
针对末位带空格,通过StringBuffer或者StringBuilder来实现存储,并能够进行多次修改
通过StringBuffer实例
.substring(0,StringBuffer实例
.length()-1)实现将末位空格去除,再统一输出
五、其他类型转String
直接调用String
.valueOf()进行转换
也使用可以使用
int+“”;转成字符串
通过String
.chatAt()获取下标元素
通过String
.substring()截取i之后的元素
通过String
.split(“”
)分隔字符串得到数组
数据结构
public class ListNode {
public int val
;
public ListNode next
;
public ListNode(int val
) {
this.val
= val
;
this.next
= null
;
}
}
public class TreeNode {
public int val
;
public TreeNode left
, right
;
public TreeNode(int val
) {
this.val
= val
;
this.left
= null
;
this.right
= null
;
}
}
Queue
<Integer> q
= new LinkedList<Integer>();
int qLen
= q
.size();
Insert
add(e
) offer(e
)
Remove
remove() poll()
Examine
element() peek()
Deque
<Integer> deque
= new ArrayDeque<Integer>();
Deque
<Integer> deque
= new LinkedList<Integer>();
arraylist
double[] myList
= new double[size
];
myList
[0] = 5.6;
double total
= 0;
for (int i
= 0; i
< size
; i
++) {
total
+= myList
[i
];
}
List
<String> list
= new ArrayList<String>();
list
.add("abc");
boolean isEmpty
= list
.isEmpty();
int size
= list
.size();
boolean isExist
= list
.contains("abc");
int index
= list
.indexOf("abc");
int lastIndex
= list
.lastIndexOf("abc");
Object
[] objectArray
= list
.toArray();
for(Object obj
: objectArray
){
System
.out
.println(obj
);
}
String s
= list
.get(0);
String old
= list
.set(0, "abc");
list
.add(0, "ghi");
list
.remove(0)
set/hash
Set
<String> hash
= new HashSet<String>();
hash
.add("billryan");
hash
.contains("billryan");
Map
<String, Integer> map
= new HashMap<String, Integer>();
map
.put("bill", 98);
map
.put("ryan", 99);
boolean exist
= map
.containsKey("ryan");
int point
= map
.get("bill");
int point
= map
.remove("bill")
Set
<String> set
= map
.keySet();
for (Map
.Entry
<String, Integer> entry
: map
.entrySet()) {
String key
= entry
.getKey();
int value
= entry
.getValue();
}
java程序
public class HelloWorld {
public static void main(String
[] args
) {
System
.out
.println("Hello World");
}
}
循环
int x
= 10;
while( x
< 20 ) {
System
.out
.print("value of x : " + x
);
x
++;
System
.out
.print("\n");
}
do {
}while(布尔表达式
);
for(int x
= 10; x
< 20; x
= x
+1) {
System
.out
.print("value of x : " + x
);
System
.out
.print("\n");
}
int [] numbers
= {10, 20, 30, 40, 50};
for(int x
: numbers
){
System
.out
.print( x
);
System
.out
.print(",");
}
System
.out
.print("\n");
String
[] names
={"James", "Larry", "Tom", "Lacy"};
for( String name
: names
) {
System
.out
.print( name
);
System
.out
.print(",");
}
if(布尔表达式
1){
}else if(布尔表达式
2){
}else {
}
switch(expression
){
case value
:
break;
default :
}
数据类型
int a
= 5000;
float b
= 13.65f;
byte c
= 0x4a;
输出
System
.out
.printf("浮点型变量的值为 " +
"%f, 整型变量的值为 " +
" %d, 字符串变量的值为 " +
"is %s", floatVar
, intVar
, stringVar
);
String fs
;
fs
= String
.format("浮点型变量的值为 " +
"%f, 整型变量的值为 " +
" %d, 字符串变量的值为 " +
" %s", floatVar
, intVar
, stringVar
);
其他
是Scanner in
=new Scanner(System
.in
);
int n
=in
.nextInt();
String s
=in
.nextLine();
BufferedReader br
= new BufferedReader(new InputStreamReader(System
.in
));
String n
=inBufferedReader
.readLine();
max
= Integer
.MAX_VALUE
;
min
= Integer
.MIN_VALUE
;
Integer
.valueOf(string
);
Integer
.parseInt(string
);
String s
="xxxx";
char[]ch
=s
.toCharArray();
s
.subString(i
,j
);
String
[]ss
=s
.split("regex");
Map
<String,String> map
= new HashMap<>();
map
.put("key","value");
map
.getOrDefault("key","default");
map
.get("key");
map
.containsKey("key");
for(Map
.Entry
<String,String> entry
: map
.entrySet()){
entry
.getKey();
entry
.getValue();
}
map
.forEach((k
, v
) -> System
.out
.println(k
+ " : " + (v
+ 10)));
Map
<Integer, Integer> map
= new HashMap<Integer, Integer>();
for (Integer key
: map
.keySet()) {
Integer value
= map
.get(key
);
System
.out
.println("Key = " + key
+ ", Value = " + value
);
}
Iterator hmIterator
= map
.entrySet().iterator();
while (hmIterator
.hasNext()) {
Map
.Entry mapElement
= (Map
.Entry
)hmIterator
.next();
int marks
= ((int)mapElement
.getValue() + 10);
System
.out
.println(mapElement
.getKey() + " : " + marks
);
}
Queue
<Integer> q
= new LinkedList<>();
q
.peek();
q
.poll();
q
.size();
q
.poll()
PriorityQueue pQueue
= new riorityQueue();
Iterator itr
= pQueue
.iterator();
while (itr
.hasNext())
System
.out
.println(itr
.next());