JAVA笔记十(40-43 数组和类集框架)

来源:互联网 发布:2017网络教育统考时间 编辑:程序博客网 时间:2024/04/28 06:46

四十、java当中的数组

数组通常用来存储一系列相同类型的数据。

1.数组的类型

int arr []  的数据类型不是整型的,是整型数组类型

2.数组的定义方法

一维数组的定义:

class Test{public static void main(String[] args){//数组的静态声明法int arr [] ={5,2,7,4,6};  //或者 int [] arr ={5,2,7,4,6};arr[3] = 10;  //数组的第一个元素的下标为0.System.out.println(arr[3]);//打印数组中的所有元素for(int i = 0; i < arr.length;i++){System.out.println(arr[i]);}//数组的动态声明法//int arr [] = new int[10];   //整型的默认值为0}}

二位数组的定义方法:

class Test{public static void main(String[] args){//二位数组的定义方法int arr [][] ={{1,2,3},{4,5,6},{7,8}};//二维数组的动态声明法//int arr1 [][] = new int[3][5];for(int i = 0;i < arr.length;i++){for(int j = 0; j < arr[i].length;j++){System.out.println(arr[i][j]);}}}}

3.数组的操作方法

所有的数据类型都可以定义数组。数组一旦声明,长度就确定了

4.复制数组的方法

The System class has an arraycopy() method that you can use to efficiently copy data from one array into another:

public static void arraycopy(Object src, int srcPos,                             Object dest, int destPos, int length)

The two Object arguments specify the array to copy from and the array to copyto. The three int arguments specify the starting position in the source array, the starting position in the destination array, and the number of array elements to copy.

The following program, ArrayCopyDemo, declares an array of char elements, spelling the word "decaffeinated." It uses theSystem.arraycopy() method to copy a subsequence of array components into a second array:

class ArrayCopyDemo {    public static void main(String[] args) {        char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',    'i', 'n', 'a', 't', 'e', 'd' };        char[] copyTo = new char[7];        System.arraycopy(copyFrom, 2, copyTo, 0, 7);        System.out.println(new String(copyTo));    }}

The output from this program is:

caffein

四十一、类集框架(collection framework)(一)

1.什么是类集框架

1)累积框架是一组类和接口;  2)位于java.util包当中; 3)主要用于存储和管理对象;  4)主要分为三大类——集合、列表和映射。

2.集合的种类

set:set中的对象不按特定的方式排序,并且没有重复对象;

list:list中对象按照索引位置排序,可以有重复对象;

map:map中的每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。

3.类集框架的基础结构

类集框架主体结构:

list的应用:

import java.util.List;import java.util.ArrayList;public class Test{public static void main(String[] args){ArrayList<String> al = new ArrayList<String>();al.add("a");al.add("b");al.add("c");al.add("d");al.remove(1);for(int i = 0; i < al.size();i++){String s = al.get(i);System.out.println(s);}al.clear();System.out.println(al.size());}}

四十二、类集框架(二)

1.Collection和Iterator接口

1)boolean add(Object o)   向集合当中加入一个对象

2)void  clear()  删除集合当中的所有对象

3)boolean isEmpty()  从集合中删除一个对象的引用

4)int size()  返回集合中元素的数目

5)remove(Object o)  从集合中删除一个对象的引用

2.Set与HashSet的使用方法

import java.util.Set;import java.util.HashSet;public class Test{public static void main(String[] args){Set<String> set = new HashSet<String>();set.add("a");set.add("b");set.add("c");set.add("d");set.add("c");System.out.println("before remove, the size is "+set.size());//结果是 4 ,因为Set中不允许重复。set.remove("a");System.out.println("after remove,the size is "+set.size());boolean b1 = set.isEmpty();System.out.println(b1);set.clear();System.out.println("after clear, the size is "+set.size());}}

运行结果:


遍历Set中元素的方法:

import java.util.Set;import java.util.HashSet;import java.util.Iterator;public class Test{public static void main(String[] args){Set<String> set = new HashSet<String>();set.add("a");set.add("b");set.add("c");set.add("d");set.add("c");//Iterator <-- Collection <-- Set <--HashSet//  <--List <--ArrayList//hasNext() next()//调用Set对象的Iterator方法,会生成一个迭代器对象,该对象用于遍历整个SetIterator<String> it = set.iterator();while(it.hasNext()){//hasNext()的作用是判断游标的后面还有没有元素,并返回一个boolean值String s = it.next();  //next()的作用是把元素取出来,并把游标往后移一位。System.out.println(s); //Set中的元素是无序的。}}}

迭代器模式:思路是在不暴露数据存储细节的情况下,能把数据结构中的对象一个个取出来。

四十三、类集框架(三)

1、Map与HashMap的使用方法

import java.util.Map;import java.util.HashMap;class Test{public static void main(String[] ars){Map<String,String> hMap = new HashMap<String,String>();hMap.put("1","a");hMap.put("2","b");hMap.put("3","c");hMap.put("2","g");int i = hMap.size();System.out.println("the size of this object is "+i);}}


2、JDK帮助文档

下载:http://www.allimant.org/javadoc/

多看帮助文档,帮组理解JDK内库。

原创粉丝点击