Java的List模板类型与C++的list模板类型的比较

来源:互联网 发布:c语言创建线程 编辑:程序博客网 时间:2024/06/01 19:12

http://blog.csdn.net/leopardaa521/article/details/5736825

Java的List只是一个容器接口,定义List必须用一些实现类如ArrayList来实现。

对List使用需要理解几个关键点:

    ①List的add()方法只能添加对象,无法添加基本数据类型!

    ②List不会自己去开辟空间来复制add()添加进来的对象,只是存这个对象的地址,用的时候再去查找对象取需要的值。所以将一个对象添加进List,这个对象在外部被修改后,List取值也会跟着变化。

    ③List允许重复添加对象,并且也为重复对象也开辟一个变量保存地址。

[java] view plaincopy
  1. import java.util.ArrayList;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4. public class Demo {  
  5.     public static void main(String[] args) {  
  6.         // TODO Auto-generated method stub  
  7.         List<int[]> list = new ArrayList<int[]>();  
  8.         for (int i = 0; i < 3; i++) {  
  9.             int[] value = new int[3];  
  10.             for (int j = 0; j < 2; j++) {  
  11.                 value[j] = j;  
  12.             }  
  13.             value[2] = i;  
  14.             list.add(value);  
  15.         }  
  16.         // 遍历List成员的成员  
  17.         Iterator<int[]> itr = list.iterator();  
  18.         while (itr.hasNext()) {  
  19.             int[] object = itr.next();  
  20.             for (int i = 0; i < object.length; i++) {  
  21.                 System.out.print(object[i]);  
  22.                 System.out.print(", ");  
  23.             }  
  24.             System.out.print("/n");  
  25.         }  
  26.     }  
  27. }  
 

 

 

我们可以拿Java的List与C++标准模板库的list做一下对比。

①下面是我从别处找来的一段代码,有兴趣的读者可以把添加对象的代码提出来,专门做成一个变量添加,然后再修改这个变量,最后打印输出,会发现C++的list是保存了push进去的对象,而不是像Java一样存的是地址。

②C++的list也允许重复添加。

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <list>  
  3. #include <string>  
  4. #include <algorithm>  
  5. using namespace std;  
  6. class employee {  
  7. private:  
  8.     long ID; //雇员编号  
  9.     string name; //雇员名称  
  10.     float salary; //雇员薪水  
  11. public:  
  12.     // 构造函数  
  13.     employee(long ID, string name, float salary) :ID(ID), name(name), salary(salary) {}  
  14.     // 成员变量的set(),get()方法  
  15.     long getID() const {  
  16.         return ID;  
  17.     }  
  18.     string getName() const {  
  19.         return name;  
  20.     }  
  21.     float getSalary() const {  
  22.         return salary;  
  23.     }  
  24.     void setID(long id) {  
  25.         (*this).ID = id;  
  26.     }  
  27.     void setName(string name){  
  28.         (*this).name = name;  
  29.     }  
  30. };  
  31. // 薪水比较  
  32. bool lessThan(const employee &a) {  
  33.     return (a.getSalary() <= 8000);  
  34. }  
  35. bool greaterThan(const employee &a) {  
  36.     return (a.getSalary() > 8000);  
  37. }  
  38. typedef list<employee> EMPLOYEE_LIST;  
  39. typedef list<employee>::iterator EMPLOYEE_IT;  
  40. typedef list<employee>::reverse_iterator REVERSE_EMPLOYEE_IT;  
  41. //output_list函数正向输出list容器对象内的所有元素  
  42. void output_list(EMPLOYEE_LIST employ) {  
  43.     EMPLOYEE_IT employit;  
  44.     for (employit = employ.begin(); employit != employ.end(); employit++) {  
  45.         cout << (*employit).getID() << '/t' << (*employit).getName() << '/t'  
  46.                 << (*employit).getSalary() << endl;  
  47.     }  
  48. }  
  49. //reverse_output_list函数逆向输出list 容器对象内的所有元素  
  50. void reverse_output_list(EMPLOYEE_LIST employ) {  
  51.     REVERSE_EMPLOYEE_IT employit;  
  52.     for (employit = employ.rbegin(); employit != employ.rend(); employit++) {  
  53.         cout << (*employit).getID() << '/t' << (*employit).getName() << '/t'  
  54.                 << (*employit).getSalary() << endl;  
  55.     }  
  56. }  
  57. int main(int argc, char* argv[]) {  
  58.     EMPLOYEE_LIST employ; //Create A Empty Queue  
  59.     EMPLOYEE_IT employit; //Create A Random Access Iterator  
  60.     //下面的四条语句分别构造一个employee对象,并插入到list容器对象  
  61.     employ.push_back(employee(100, "张三", 10000));  
  62.     employ.push_back(employee(101, "李四", 8000));  
  63.     employ.push_back(employee(102, "王五", 8800));  
  64.     employ.push_front(employee(108, "大郎", 5000));  
  65.     //正向输出list容器对象的所有元素  
  66.     output_list(employ);  
  67.     cout << "-----------------------------------------" << endl;  
  68.     //按compare谓词删除容器中的元素  
  69.     employ.remove_if(lessThan);  
  70.     output_list(employ);  
  71.     cout << "-----------------------------------------" << endl;  
  72.     //逆向输出list容器对象的所有元素  
  73.     reverse_output_list(employ);  
  74.     //统计并输出工资高于8000的员工人数  
  75.     int i = count_if(employ.begin(), employ.end(), greaterThan);  
  76.     cout << "工资高于8000元的有" << i << "位!" << endl;  
  77.     return 0;  
  78. }  


0 0