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

JAVASE-学习日志3

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

JAVASE-学习日志3

一、JAVA集合

java集合的主要分为三种类型:

Set,Map,List.

1.Set集合

Set是一个可以放置对象的集合,但是通常不能记住添加元素的顺序。

Set集合不允许包含相同的元素,如果试图把两个相同元素加入同一个Set集合中,则添加操作失败,add()方法返回false,且新元素不会被加入。

主要有HashSet和TreeSet两大实现类。

(1)HashSet类

HashSet是Set接口的典型实现,大多数时候使用Set集合时就是使用这个实现类。HashSet按Hash算法来存储集合中的元素,因此具有很好的存取和查找性能。底层数据结构是哈希表。

HashSet类的一些重要方法:

import java.util.HashSet;//包

1、add方法,向HashSet集合中添加元素;

HashSet set = new HashSet();
//调用HashSet无参构造方法——>创建HashMap对象并给map全局变量
set.add("aa");
//向HashSet集合中添加元素.
//实质是调用put方法,
//将数据存到了key——→因为HashMap 的 key 不允许重复,所以HashSet添加的元素也不允许重复
set.add("bb");
//注意:不会按照保存的顺序存储数据;遍历是不能保证下次结果和上次相同

2、size() :返回Set集合中的元素个数;

set.size();//

3、isEmpty() :如果Set不包含元素,则返回 true ,否则返回false;

set.isEmpty();

4、remove(Object  obj)方法,删除Set集合中的元素,删除成功返回true,否则返回false;

set.remove("aa");

 5、clear() : 移除此Set中的所有元素;

set.clear();

6、contains(Object o):如果Set包含指定的元素,则返回 true,否则返回false;

set.contains("aa");

7、iterator() :返回在此Set中的元素上进行迭代的迭代器;

import java.util.Iterator;//包
Iterator iterator = set.iterator();//将HashSet中数据转存至iterator
while(iterator.hasNext()) {
System.out.println(iterator.next());}
(2)TreeSet类 

TreeSet类实现了SortedSet接口,能够对集合中的对象进行排序。 

与HashSet集合相比,TreeSet还提供了几个额外方法:

Comparator comparator():如果TreeSet采用了定制顺序,则该方法返回定制排序所使用的Comparator,如果TreeSet采用自然排序,则返回null;
Object first():返回集合中的第一个元素;
Object last():返回集合中的最后一个元素;
Object lower(Object e):返回指定元素之前的元素。
Object higher(Object e):返回指定元素之后的元素。
SortedSet subSet(Object fromElement,Object toElement):返回此Set的子集合,含头不含尾;
SortedSet headSet(Object toElement):返回此Set的子集,由小于toElement的元素组成;
SortedSet tailSet(Object fromElement):返回此Set的子集,由大于fromElement的元素组成;

TreeSet nums = new TreeSet<>();
//向集合中添加元素
nums.add(5);
nums.add(2);
nums.add(15);
nums.add(-4);
//输出集合,可以看到元素已经处于排序状态
System.out.println(nums);//[-4, 2, 5, 15]
System.out.println("集合中的第一个元素:"+nums.first());//集合中的第一个元素:-4
System.out.println("集合中的最后一个元素:"+nums.last());//集合中的最后一个元素:15
System.out.println("集合小于4的子集,不包含4:"+nums.headSet(4));//集合小于4的子集,不包含4:[-4, 2]
System.out.println("集合大于5的子集:"+nums.tailSet(2));//集合大于5的子集:[2, 5, 15]
System.out.println("集合中大于等于-3,小于4的子集:"+nums.subSet(-3,4));//集合中大于等于-3,小于4的子集:[2]

TreeSet支持两种排序方法:自然排序和定制排序,在默认情况下,采用的是自然排序。

Java提供了一个Comparable接口,该接口里定义了一个compareTo(Object obj)方法,该方法返回一个整数值,实现该接口的类必须实现该方法,实现了该接口的类必须实现该方法,实现接口的类就可以比较大小了。当调用一个一个对象调用该方法与另一个对象进行比较时, compareTo(Object obj)如果返回0表示两个对象相等;如果返回正整数则表明obj1大于obj2,如果是负整数则相反。

TreeSet的自然排序是根据集合元素的大小,TreeSet将它们以升序排列。如果需要实现定制排序,例如降序排序,则可通过Comparator接口的帮助。该接口里包含一个int compare(T o1,T o2)方法,用于比较o1和o2的大小。由于Comparator是一个函数式接口,因此还可以使用Lambda表达式来代替Comparator子类对象。

2.Map类 

将键映射到值的对象。
一个映射不能包含重复的键。
每个键最多只能映射到一个值。
Map接口和Collection接口的不同。
Map是双列的,Collection是单列的。
Map的键唯一,Collection的子体系Set是唯一的。
Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效。

Map类一些重要的方法; 

//添加功能
V put(K key,V value);//添加元素。这个其实还有另一个功能?替换
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

//删除功能
void clear();//移除所有的键值对元素
V remove(Object key);//根据键删除键值对元素,并把值返回

//判断功能
boolean containsKey(Object key);//判断集合是否包含指定的键
boolean containsValue(Object value);//判断集合是否包含指定的值
boolean isEmpty();//判断集合是否为空

//获取功能
Set> entrySet();//返回一个键值对的Set集合
V get(Object key);//根据键获取值
Set keySet();//获取集合中所有键的集合
Collection values();//获取集合中所有值的集合

//长度功能
int size();//返回集合中的键值对的对数
public class Demo1 {
	public static void main(String[] args) {
		// 定义一个Map的容器对象
		Map map1 = new HashMap();
		map1.put("jack", 20);
		map1.put("rose", 18);
		map1.put("lucy", 17);
		map1.put("java", 25);
		System.out.println(map1);
		// 添加重复的键值(值不同),会返回集合中原有(重复键)的值,		 System.out.println(map1.put("jack", 30)); //20
		       
		Map map2 = new HashMap();
		map2.put("张三丰", 100);
		map2.put("虚竹", 20);
		System.out.println("map2:" + map2);
// 从指定映射中将所有映射关系复制到此映射中。
		map1.putAll(map2);
		System.out.println("map1:" + map1);
         //
	}
}

// 删除:
		// remove() 删除关联对象,指定key对象
		// clear() 清空集合对象
 
		Map map1 = new HashMap();
		map1.put("jack", 20);
		map1.put("rose", 18);
		map1.put("lucy", 17);
		map1.put("java", 25);
		System.out.println(map1);				
// 指定key,返回删除的键值对映射的值。
		System.out.println("value:" + map1.remove("java"));
		map1.clear();
		System.out.println("map1:" + map1);


// 获取:
		// V get(Object key) 通过指定的key对象获取value对象
		// int size() 获取容器的大小
		Map map1 = new HashMap();
		map1.put("jack", 20);
		map1.put("rose", 18);
		map1.put("lucy", 17);
		map1.put("java", 25);
		System.out.println(map1);
		// V get(Object key) 通过指定的key对象获取value对象
		// int size() 获取容器的大小
		System.out.println("value:" + map1.get("jack"));
		System.out.println("map.size:" + map1.size());


// 判断:
		// boolean isEmpty() 长度为0返回true否则false
		// boolean containsKey(Object key) 判断集合中是否包含指定的key
		// boolean containsValue(Object value)
 
		Map map1 = new HashMap();
		map1.put("jack", 20);
		map1.put("rose", 18);
		map1.put("lucy", 17);
		map1.put("java", 25);
		System.out.println(map1);
		System.out.println("isEmpty:" + map1.isEmpty());
		System.out.println("containskey:" + map1.containsKey("jack"));
		System.out.println("containsvalues:" + map1.containsValue(100));
(1)HashMap类

以哈希表数据结构实现,查找对象时通过哈希函数计算其位置,它是为快速查询而设计的,其内部定义了一个hash表数组(Entry[] table),元素会通过哈希转换函数将元素的哈希地址转换成数组中存放的索引,如果有冲突,则使用散列链表的形式将所有相同哈希地址的元素串起来,可能通过查看HashMap.Entry的源码它是一个单链表结构。

public class Test4 {
    public static void main(String[] args) {
        HashMap map = new HashMap<>();
        map.put(new Phone("Apple",7000),"美国");
        map.put(new Phone("Sony",5000),"日本");
        map.put(new Phone("Huawei",6000),"中国");
        Set phones = map.keySet();
        Iterator iterator = phones.iterator();
        while (iterator.hasNext()){
            Phone next = iterator.next();
            System.out.println(next.getBrand()+"=="+next.getPrice()+"=="+map.get(next));
        }

    }
}
class Phone{
    private String Brand;
    private int Price;

    public Phone(String brand, int price) {
        Brand = brand;
        Price = price;
    }

    public String getBrand() {
        return Brand;
    }

    public void setBrand(String brand) {
        Brand = brand;
    }

    public int getPrice() {
        return Price;
    }

    public void setPrice(int price) {
        Price = price;
    }
}

(2)TreeMap类 

 基于红黑树数据结构的实现。查看“键”或“键值对”时,它们会被排序(次序由Comparabel或Comparator决定)。TreeMap的特点在 于,你得到的结果是经过排序的。TreeMap是唯一的带有subMap()方法的Map,它可以返回一个子树。

public class Demo4 {
	public static void main(String[] args) {
		TreeMap tree = new TreeMap();
		tree.put("张三", 19);
		tree.put("李四", 20);
		tree.put("王五", 21);
		tree.put("赵六", 22);
		tree.put("周七", 23);
		tree.put("张三", 24);
		System.out.println(tree);
		System.out.println("张三".compareTo("李四"));//-2094
	}
}
3.List类 

List是一个继承于Collection的接口,即List是集合中的一种。List是有序的队列,List中的每一个元素都有一个索引;第一个元素的索引值是0,往后的元素的索引值依次+1。和Set不同,List中允许有重复的元素。

 List集合类中元素有序(添加顺序和取出顺序一致),且可重复
List集合中的每个元素都有其对应的顺序索引,即支持索引
List容器中的元素都对应一个整数型的序号记载其在容器中的位置,额可以根据序号存取容器中的元素
JDK API中List接口的实现类非常多,常用的有ArrayList、Set、Vector

List类的一些常用方法:

(1)ArrayList类

ArrayList底层是用数组实现的,可以认为ArrayList是一个可改变大小的数组。随着越来越多的元素被添加到ArrayList中,其规模是动态增加的。

ArrayList arrayList = new ArrayList();
// ArrayList add
long startTime = System.nanoTime();
 
for (int i = 0; i < 100000; i++) {
    arrayList.add(i);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println("ArrayList add:  " + duration);


// ArrayList get
startTime = System.nanoTime();
 
for (int i = 0; i < 10000; i++) {
    arrayList.get(i);
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("ArrayList get:  " + duration);


 
// ArrayList remove
startTime = System.nanoTime();
 
for (int i = 9999; i >=0; i--) {
    arrayList.remove(i);
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("ArrayList remove:  " + duration);




(2)LinkedList类

LinkedList底层是通过双向链表实现的。所以,LinkedList和ArrayList之前的区别主要就是数组和链表的区别。

LinkedList linkedList = new LinkedList();
// LinkedList add
startTime = System.nanoTime();
 
for (int i = 0; i < 100000; i++) {
    linkedList.add(i);
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("LinkedList add: " + duration);


// LinkedList get
startTime = System.nanoTime();
 
for (int i = 0; i < 10000; i++) {
    linkedList.get(i);
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("LinkedList get: " + duration);

// LinkedList remove
startTime = System.nanoTime();
 
for (int i = 9999; i >=0; i--) {
    linkedList.remove(i);
}
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("LinkedList remove: " + duration);

最后的结果

ArrayList add:  13265642
LinkedList add: 9550057
ArrayList get:  1543352
LinkedList get: 85085551
ArrayList remove:  199961301
LinkedList remove: 85768810
二、JAVA常用类  1.Object类

Java Object 类是所有类的父类,也就是说 Java 的所有类都继承了 Object,子类可以使用 Object 的所有方法。

Object 类位于 java.lang 包中,编译时会自动导入,我们创建一个类时,如果没有明确继承一个父类,那么它就会自动继承 Object,成为 Object 的子类。

显式继承:
public class Runoob extends Object{

}
隐式继承:
public class Runoob {

}

Object类一些常用的方法:

1. clone()

保护方法,实现对象的浅复制,只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedException异常。

2. getClass()

final方法,返回Class类型的对象,反射来获取对象。

3. toString()

该方法用得比较多,一般子类都有覆盖,来获取对象的信息。

4. finalize()

该方法用于释放资源。因为无法确定该方法什么时候被调用,很少使用。

5. equals()

比较对象的内容是否相等

6. hashCode()

该方法用于哈希查找,重写了equals方法一般都要重写hashCode方法。这个方法在一些具有哈希功能的Collection中用到。

7. wait()

wait方法就是使当前线程等待该对象的锁,当前线程必须是该对象的拥有者,也就是具有该对象的锁。wait()方法一直等待,直到获得锁或者被中断。wait(long timeout)设定一个超时间隔,如果在规定时间内没有获得锁就返回。

调用该方法后当前线程进入睡眠状态,直到以下事件发生。

其他线程调用了该对象的notify方法。
其他线程调用了该对象的notifyAll方法。
其他线程调用了interrupt中断该线程。
时间间隔到了。
此时该线程就可以被调度了,如果是被中断的话就抛出一个InterruptedException异常。

8. notify()

该方法唤醒在该对象上等待的某个线程。

9. notifyAll()
2.String类 

字符串广泛应用 在 Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。

String类的一些常用方法

1.求字符串的长度

public int length() // 返回该字符串的长度

String str = "Hello";
int strlength = str.length();   // 5

2、求字符串某一位置字符
public char charAt(int index)   //   返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。

String str = "Hello";
char ch = str.charAt(4);//ch = o

3、提取子串
用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
1)public String substring(int beginIndex) // 该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
2)public String substring(int beginIndex, int endIndex) // 该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。

String str1 = "Hello";
String str2 = str1.substring(2); // str2 = "llo"
String str3 = str1.substring(2,4);  //str3 = "ll"

4、字符串比较
1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。
3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。

String str1 = "abc";
String str2 = "ABC";
int a = str1.compareTo(str2); //a>0
int b = str1.compareToIgnoreCase(str2); //b=0
boolean c = str1.equals(str2); //c=false
boolean d = str1.equalsIgnoreCase(str2); //d=true

5、字符串连接
public String concat(String str) // 将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。

String str = "Hello".concat("World").concat("!");  // HelloWorld!

6、字符串中单个字符或字符串的查找
1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。

 String str = "I am a good student";
 int a = str.indexOf('a');//a = 2
 int b = str.indexOf("good");//b = 7
 int c = str.indexOf("w",2);//c = -1
 int d = str.lastIndexOf("a");//d = 5
 int e = str.lastIndexOf("a",3);//e = 2

7、字符串中字符的大小写转换
1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串

 String str = new String("Hello");
 String str1 = str.toLowerCase();//str1 = "hello"
 String str2 = str.toUpperCase();//str2 = "HELLO"

8、字符串中字符的替换
1)public String replace(char oldChar, char newChar) //用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。

 String str = "Hello";
 String str1 = str.replace('e','E');//str1 = "HEllo"
 String str2 = str.replaceFirst("He","qw");//str2 = "qwllo"
 String str3 = str.replaceAll("He","df");//str3 = "dfllo"

9.其他方法

1)String trim //忽略字符串前导和尾部的空格,返回字符串

String s = " Hello  ";
System.out.println(s.trim()); // s = "Hello"
2)isEmpty()  // 判断字符串是否为空。

String s = " Hello  ";
System.out.println(s.isEmpty()); // s = "false"
3) contains()  //判断是否包含指定的字符串

String s = " Hello  ";
System.out.println(s.contains("ll")); // s = "true"
4) char[] toCharArray() // 将此字符串转换为一个新的字符数组。

5)boolean startsWith(String prefix) // 测试此字符串是否以指定的子串开始。

6)  boolean endsWith(String prefix) // 测试此字符串是否以指定的子串结束。 

7)String[] split(String regex,int limit) // 根据匹配给定的正则表达式来拆分此字符串

3.StringBuilder类 

String对象是不可改变的。每次使用 System.String类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。在需要对字符串执行重复修改的情况下,与创建新的 String对象相关的系统开销可能会非常昂贵。如果要修改字符串而不创建新的对象,则可以使用System.Text.StringBuilder类。例如,当在一个循环中将许多字符串连接在一起时,使用 StringBuilder类可以提升性能。

StringBuilder类一些常用的方法

//1.Append方法可用来将文本或对象的字符串表示形式添加到由当前 StringBuilder对象表示的字符串的结尾处。以下示例将一个StringBuilder对象初始化为“Hello World”,然后将一些文本追加到该对象的结尾处。将根据需要自动分配空间。

StringBuilderMyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder.Append(" What a beautiful day.");
Console.WriteLine(MyStringBuilder);

//2.AppendFormat方法将文本添加到 StringBuilder的结尾处,而且实现了 IFormattable接口,因此可接受格式化部分中描述的标准格式字符串。可以使用此方法来自定义变量的格式并将这些值追加到 StringBuilder的后面。以下示例使用 AppendFormat方法将一个设置为货币值格式的整数值放置到 StringBuilder的结尾。

int MyInt= 25;
StringBuilder MyStringBuilder = new StringBuilder("Your total is ");
MyStringBuilder.AppendFormat("{0:C} ", MyInt);
Console.WriteLine(MyStringBuilder);

//3.Insert方法将字符串或对象添加到当前 StringBuilder中的指定位置。以下示例使用此方法将一个单词插入到 StringBuilder的第六个位置。

StringBuilderMyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder.Insert(6,"Beautiful ");
Console.WriteLine(MyStringBuilder);

//4.可以使用 delete方法从当前 StringBuilder中移除指定数量的字符,移除过程从指定的从零开始的索引处开始。以下示例使用 Remove方法缩短 StringBuilder。

StringBuilderMyStringBuilder = new StringBuilder("helloooo");
MyStringBuilder.delete(5,7);
Console.WriteLine(MyStringBuilder);

//5.使用 Replace方法,可以用另一个指定的字符来替换 StringBuilder对象内的字符。以下示例使用 Replace方法来搜索 StringBuilder对象,查找所有的感叹号字符 (!),并用问号字符 (?)来替换它们。

StringBuilderMyStringBuilder = new StringBuilder("Hello World!");
MyStringBuilder.Replace('!', '?');
Console.WriteLine(MyStringBuilder);
4.StringBuffer类 

StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于String类。

所以在实际使用时,如果经常需要对一个字符串进行修改,例如插入、删除等操作,使用StringBuffer要更加适合一些。

在StringBuffer类中存在很多和String类一样的方法,这些方法在功能上和String类中的功能是完全一样的。

但是有一个最显著的区别在于,对于StringBuffer对象的每次修改都会改变对象自身,这点是和String类最大的区别。

另外由于StringBuffer是线程安全的,关于线程的概念后续有专门的章节进行介绍,所以在多线程程序中也可以很方便的进行使用,但是程序的执行效率相对来说就要稍微慢一些。

 

 StringBuffer类一些常用的方法

StringBuffer类有三个构造方法
StringBuffer();
StringBuffer(int size);
StringBuffer(String s);

1.append()——将其他Java类型(八大数据类型)数据转化为字符串后,再追加到StringBuffer对象中。
2.char charAt(int n)——得到参数n指定的索引位置上的单个字符。n的值必须是正数,并且小于当前对象的字符序列的长度。
3.void setCharAt(int n,char ch)——将当前StringBuffer对象实体中的字符序列的位置n处的字符用参数ch指定的字符替换。n的值必须是正数,并且小于当前对象的字符序列的长度。
4.StringBuffer insert(int index,String str)——StringBuffer对象使用insert方法将参数str指定的字符序列插入参数index指定的位置,并返回当前对象的引用。
5.public StringBuffer reverse()——StringBuffer对象使用reserve方法将该对象实体中的字符序列翻转,并返回当前对象的引用。
6.StringBuffer delete(int startIndex,int endIndex)——delete(int startIndex,int endIndex)从当前StringBuffer对象实体中的字符序列中删除一个子字符序列,并返回当前对象的引用。这里startIndex指定了需删除的第一个字符的下标,而endIndex指定了需删除的最后一个字符下标是endIndex-1,即要删除的子字符序列从startIndex到endIndex-1。deleteCharAt(int index)方法删除当前StringBuffer对象的字符序列中index位置处的一个字符。
7.StringBuffer replace(int startIndex,int endIndex,String str)——replace(int startIndex,int endIndex,String str)方法将当前StringBuffer对象的startIndex到endIndex-1的字符序列替换成参数str指定的字符序列,并返回当前StringBuffer对象的引用。
5.System类

java.lang.System`类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作。

 System类一些常用的方法

public static void exit(int status)  系统退出 ,如果status为0就表示退出。
public static void gc()   运行垃圾收集机制,调用的是Runtime类中的gc方法。
public static long currentTimeMillis()  返回以毫秒为单位的当前时间。
public static void arraycopy(Object src,int srcPos, Object dest,int desPos,int length) 数组拷贝操作
public static Properties getProperties() 取得当前系统的全部属性。
public static String  getProperty(String key) 根据键值取得属性的具体内容。
//System类里面有两个回去时间的方式,但他们有区别,并且区别还多大的,先看使用方法。
long timeMill = System.currentTimeMillis();
System.out.println("当前系统毫秒时间:" + timeMill); //当前系统毫秒时间:1608523828707
long timeNano = System.nanoTime();
System.out.println("当前系统纳秒时间:" + timeNano); //当前系统纳秒时间:2945068891755425

System.getProperties().list(System.out) ;	// 列出系统的全部属性

6.Data类 

java.util.Data类,表示特定的瞬间。精确到毫秒。

Data类一些常用的方法 

public Date()//分配Data对象并初始化此对象。以表示分配它的时间(精确到毫秒)。
public Data(Long data)//分配Data对象并初始化此对象,以表示自从基准时间(成为“历元”(epoch)),即1970年1月1日00:00:00 GTM)以来的指定毫秒数。
public Long getTime()//把日期对象转换成对应的时间毫秒值。
public String format(Data data)//将Data对象格式化为字符串。
public Data parse(String source)//将字符串解析为Data对象。
public int get(int field)//返回给定日历字段的值。
public void set (int field,int value)//将给定的日历字段设置为给定值。
public abstract void add(int field,int amount)//根据日历的规则,为给定的日历字段添加或减去指定的时间量。

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

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

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

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