入门java新手,以前都用的c++刷题
记录java常用的api,以及刷题常用的方法(对标c++ STL标准模板库)
创建数组
dataType[] arrayRefVar;
double[] myList;
arrayRefVar = new dataType[arraySize];//使用new操作符来创建数组
dataType[] arrayRefVar = new dataType[arraySize];
For-Each 循环
JDK 1.5 引进了一种新的循环类型,被称为 For-Each 循环或者加强型循环,它能在不使用下标的情况下遍历数组。
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// 打印所有数组元素
for (double element: myList) {
System.out.println(element);
}
}
}
多维数组
String str[][] = new String[3][4];
int a[][] = new int[2][3];
int m=res.length;//行
int n=res[0].length;//列
Araays类
java.util.Arrays 类能方便地操作数组,它提供的所有方法都是静态的。
具有以下功能:
给数组赋值:通过 fill 方法。
对数组排序:通过 sort 方法,按升序。
比较数组:通过 equals 方法比较数组中元素值是否相等。
查找数组元素:通过 binarySearch 方法能对排序好的数组进行二分查找法操作。
构造方法
当一个对象被创建时候,构造方法用来初始化该对象。构造方法和它所在类的名字相同,但构造方法没有返回值。
通常会使用构造方法给一个类的实例变量赋初值,或者执行其它必要的步骤来创建一个完整的对象。
不管你是否自定义构造方法,所有的类都有构造方法,因为Java自动提供了一个默认构造方法,默认构造方法的访问修改符和类的访问修改符相同(类为 public,构造函数也为 public;类改为 private,构造函数也改为 private)。
一旦你定义了自己的构造方法,默认构造方法就会失效。
Java Scanner类
Scanner s = new Scanner(System.in);
接下来我们演示一个最简单的数据输入,并通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串,在读取前我们一般需要 使用 hasNext 与 hasNextLine 判断是否还有输入的数据:
使用next方法
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// next方式接收字符串
System.out.println("next方式接收:");
// 判断是否还有输入
if (scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
使用 nextLine 方法:
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
// nextLine方式接收字符串
System.out.println("nextLine方式接收:");
// 判断是否还有输入
if (scan.hasNextLine()) {
String str2 = scan.nextLine();
System.out.println("输入的数据为:" + str2);
}
scan.close();
}
}
正则表达式
Java核心API
1.String
String str=new String();
class CollectionDemo1 {
private static Date today = new Date();
public static void main(String[] args) {
//Collection c=new ArrayDeque();
Collection c = new ArrayList();
//Collection c= new HashSey();
c.add(today);
c.add("nowcoder");
c.add(10); // 自动装箱
System.out.println(c); // toString()
System.out.println(Arrays.toString(c.toArray()));//转换成数组
System.out.println(c.size());
System.out.println(c.contains(today));
System.out.println(c.contains("nowcoder"));
System.out.println(c.contains(10));
c.remove(today);
System.out.println(c);
c.clear();//清空
System.out.println(c);
}
}
1.HashSet
- HashSet不能保证元素的排列顺序
- HashSet集合元素的值可以为null;
- HashSet是非线程安全的,多线程环境下须通过代码来保证其同步·
HashSet student =new hashSet();//无序
student.add("唐僧“);
LinkedHashSet teachers=new LinkedHashSet();//有序
2.TreeSet
TreeSet可以保证元素的排列顺序,它比HashSet多了一些方法
1.返回集合中的第一个 (最后一个) 元素;
2.返回集合中位于指定元素之前 (之后) 的元素
3.返回集合中某个限定范围内的元素组成的子集
TreeSet采用红黑树的数据结构来存储元素,支持自然排序和定制排序
3.List
List代表有序集合,它提供了根据索引来访问集合的方法
1.将元素插入到集合中指定的索引处
2.将指定索引处的元素从集合中删除
3.从集合中返回指定索引处的元素
4.返回某个元素在集合中的索引值
List提供了list terator()方法,以返回一个ListIterator对象ListIterator接口继承于Iterator接口,
List names=new ArrayList();//最常用
names.add(1,lucy);
names.romove(1);
for(int i=0;i<names.size();++i){
System.out.println(names.get(i));
}
System.out.println(names.subList(1,4));//获取索引1-4的数据
System.out.println(names.indexOf("mary"));//获取mary第一次出现的索引
System.out.println(names.lastIndexOf("mary"));//获取maryZ最后一次出现的索引
ListIterator iterator =names.listIterator();
while(iterator.hasNext()){//从前往后遍历
System.out.println();
}
while(iterator.hasPrevious()){//从后往前遍历
System.out.println();
}
4.ArrayList
1.ArrayList是基于数组实现的List接口
2.ArrayList内部封装了一个长度可变的Object[]数组
3,默认该数组的初始长度为10,也可以通过构造器参数显示指定其初始长度
4每当添加的元素个数超出数组的长度,ArrayList会自动对长度进行扩展
5.Vector
1.Vector也是基于数组的实现,与ArrayList用法相同
2.Vector是线程安全的,ArrayList则是非线程安全的
3.Vector有保正线程安全的开销,所以性能不如ArrayList;
4.Vector的子类stack,用于模拟栈这种数据结构 (LIFO)
//Arratys.ArrayList
// int [] nums={10,20,30,40,50};//这个不行
Integer [] nums={10,20,30,40,50};//多个Integer对象
List numList=Arrays.asList(nums);
System.out.println(numList);
5.Queue
模拟队列
Queue queue =new ArrayDeque();
queue.offer("mary");
System.out.println(queue.peek());
while(queue.size()>0){
System.out.println(queue.poll());
}
6.Deque
//双端队列
Deque queue =new ArrayDeque():
queue.offer("mary");
queue.offerFirst
//栈
Deque stack=new ArrayDeque();
Map
HashMap是Map接口的典型实现
key-value类
Map map=new HashMap();
map.put("John",70);
//value值可以重复,key重复回导致覆盖
System,out pringtln(map.get("John"));
System,out pringtln(map.containsKey("John"));
System,out pringtln(map.containsValue(70));
map.remove("John");
泛型
List<String> names=new ArrayList<String>();//java5
//java 5
Set<Double>set1=new HashSet<Double>();