Java的List模板类型与C++的list模板类型的比较
2010-07-15 13:03
246 查看
一点工作心得,理解的也未必正确,不对的话大家尽管指出。
Java的List只是一个容器接口,定义List必须用一些实现类如ArrayList来实现。
对List使用需要理解几个关键点:
①List的add()方法只能添加对象,无法添加基本数据类型!
②List不会自己去开辟空间来复制add()添加进来的对象,只是存这个对象的地址,用的时候再去查找对象取需要的值。所以将一个对象添加进List,这个对象在外部被修改后,List取值也会跟着变化。
③List允许重复添加对象,并且也为重复对象也开辟一个变量保存地址。
我们可以拿Java的List与C++标准模板库的list做一下对比。
①下面是我从别处找来的一段代码,有兴趣的读者可以把添加对象的代码提出来,专门做成一个变量添加,然后再修改这个变量,最后打印输出,会发现C++的list是保存了push进去的对象,而不是像Java一样存的是地址。
②C++的list也允许重复添加。
Java的List只是一个容器接口,定义List必须用一些实现类如ArrayList来实现。
对List使用需要理解几个关键点:
①List的add()方法只能添加对象,无法添加基本数据类型!
②List不会自己去开辟空间来复制add()添加进来的对象,只是存这个对象的地址,用的时候再去查找对象取需要的值。所以将一个对象添加进List,这个对象在外部被修改后,List取值也会跟着变化。
③List允许重复添加对象,并且也为重复对象也开辟一个变量保存地址。
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Demo { public static void main(String[] args) { // TODO Auto-generated method stub List<int[]> list = new ArrayList<int[]>(); for (int i = 0; i < 3; i++) { int[] value = new int[3]; for (int j = 0; j < 2; j++) { value[j] = j; } value[2] = i; list.add(value); } // 遍历List成员的成员 Iterator<int[]> itr = list.iterator(); while (itr.hasNext()) { int[] object = itr.next(); for (int i = 0; i < object.length; i++) { System.out.print(object[i]); System.out.print(", "); } System.out.print("/n"); } } }
我们可以拿Java的List与C++标准模板库的list做一下对比。
①下面是我从别处找来的一段代码,有兴趣的读者可以把添加对象的代码提出来,专门做成一个变量添加,然后再修改这个变量,最后打印输出,会发现C++的list是保存了push进去的对象,而不是像Java一样存的是地址。
②C++的list也允许重复添加。
#include <iostream> #include <list> #include <string> #include <algorithm> using namespace std; class employee { private: long ID; //雇员编号 string name; //雇员名称 float salary; //雇员薪水 public: // 构造函数 employee(long ID, string name, float salary) :ID(ID), name(name), salary(salary) {} // 成员变量的set(),get()方法 long getID() const { return ID; } string getName() const { return name; } float getSalary() const { return salary; } void setID(long id) { (*this).ID = id; } void setName(string name){ (*this).name = name; } }; // 薪水比较 bool lessThan(const employee &a) { return (a.getSalary() <= 8000); } bool greaterThan(const employee &a) { return (a.getSalary() > 8000); } typedef list<employee> EMPLOYEE_LIST; typedef list<employee>::iterator EMPLOYEE_IT; typedef list<employee>::reverse_iterator REVERSE_EMPLOYEE_IT; //output_list函数正向输出list容器对象内的所有元素 void output_list(EMPLOYEE_LIST employ) { EMPLOYEE_IT employit; for (employit = employ.begin(); employit != employ.end(); employit++) { cout << (*employit).getID() << '/t' << (*employit).getName() << '/t' << (*employit).getSalary() << endl; } } //reverse_output_list函数逆向输出list 容器对象内的所有元素 void reverse_output_list(EMPLOYEE_LIST employ) { REVERSE_EMPLOYEE_IT employit; for (employit = employ.rbegin(); employit != employ.rend(); employit++) { cout << (*employit).getID() << '/t' << (*employit).getName() << '/t' << (*employit).getSalary() << endl; } } int main(int argc, char* argv[]) { EMPLOYEE_LIST employ; //Create A Empty Queue EMPLOYEE_IT employit; //Create A Random Access Iterator //下面的四条语句分别构造一个employee对象,并插入到list容器对象 employ.push_back(employee(100, "张三", 10000)); employ.push_back(employee(101, "李四", 8000)); employ.push_back(employee(102, "王五", 8800)); employ.push_front(employee(108, "大郎", 5000)); //正向输出list容器对象的所有元素 output_list(employ); cout << "-----------------------------------------" << endl; //按compare谓词删除容器中的元素 employ.remove_if(lessThan); output_list(employ); cout << "-----------------------------------------" << endl; //逆向输出list容器对象的所有元素 reverse_output_list(employ); //统计并输出工资高于8000的员工人数 int i = count_if(employ.begin(), employ.end(), greaterThan); cout << "工资高于8000元的有" << i << "位!" << endl; return 0; }
相关文章推荐
- Java的List模板类型与C++的list模板类型的比较
- java和c/c++ 数据类型长度的比较
- C/C++与Java各数据类型所占字节数比较
- 强弱类型,动态静态语言比较(JAVA,C,C++,Python,Ruby,PHP,Perl)
- java,c,c++ 语言之间基本数据类型的比较
- java,c,c++ 语言之间基本数据类型的比较
- Java 中的泛型与 C++ 模板的比较
- Python【基础:数据类型和变量 字符串和编码 list和tuple 条件判断 循环 dict和set】注意事项(与java,c比较)
- java,c,c++ 语言之间基本数据类型的比较
- c++的STL模板库中3种容器类:vector,list,deque的比较
- java,c,c++ 语言之间基本数据类型的比较
- java和c/c++ 数据类型长度的比较
- C/Java/C++数据类型的比较
- C/C++/Java数据类型比较及Java位操作运算讲解
- 【Java】List接口的应用,与C++中vector应用比较 (一)
- (总结)C++与java比较教程--7.2 C++中声明指针类型和指针数组(待补充)
- Java基本类型与C++基本类型的一些比较
- C++与Java基本数据类型比较
- Java 中的泛型与 C++ 模板的比较
- Java 中的泛型与 C++ 模板的比较