【DataStructure】 Five methods to init the List in java

Do you know how to init list in other way except for new object? The following will give you serveral tips. If having other great idea, you are welcome to share. 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.ArrayList;  
  2. import java.util.Arrays;  
  3. import java.util.Collections;  
  4. import java.util.List;  
  5.   
  6. public class ListUtil  
  7. {  
  8.   
  9.     /** 
  10.      *Init the List with different methods.  
  11.      *  
  12.      * @time Jul 21, 2014 5:39:10 PM 
  13.      * @return void 
  14.      */  
  15.     public static void init()  
  16.     {  
  17.         // use new to init List  
  18.         List<String> firstList = new ArrayList<String>(0);  
  19.         firstList.add("ABC");  
  20.         firstList.add("FGF");  
  21.         firstList.add("CID");  
  22.         System.out.println(firstList);  
  23.           
  24.         // use the string to init List  
  25.         List<String> testList = Arrays.asList("ABC""FGF""CID");  
  26.         System.out.println(testList);  
  27.         //output:[ABC, ABC, GFG, CID]  
  28.           
  29.         /** 
  30.          * Notes: The following methods is dependent on the above List. 
  31.          */  
  32.           
  33.         //use arrays and copy to init List  
  34.         List<String> copyList = Arrays.asList(new String[testList.size()]);  
  35.         Collections.copy(copyList,testList);  
  36.         System.out.println(copyList);  
  37.           
  38.         //use new object and copy to init List  
  39.         copyList = new ArrayList<String>();  
  40.         Collections.addAll(copyList, new String[testList.size()]);  
  41.         Collections.copy(copyList,testList);  
  42.         System.out.println(copyList);  
  43.           
  44.         //use the list.subList to init List  
  45.         List<String> strSubList = testList.subList(0, testList.size());  
  46.         System.out.println(strSubList);  
  47.     };  
  48.       
  49.     public static void main(String[] args)  
  50.     {  
  51.         ListUtil.init();  
  52.     }  
  53. }  

The result is :

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [ABC, FGF, CID]  
  2. [ABC, FGF, CID]  
  3. [ABC, FGF, CID]  
  4. [ABC, FGF, CID]  
  5. [ABC, FGF, CID]  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建一个抽象类DataStructure该类包括下面成员变量成员函数: 1) 一个成员变量len,表示里面的元素个数最大值 2) 构造函数DataStructure(int l),将len初始化为0 3) 虚析构函数~DataStructure() 4) 纯虚函数Output(),输出DataStructure中的数据 5) 纯虚函数Size(),返回DataStructure中的元素个数 2. 创建DataStructure一个派生类MyString,该类包括下面成员变量成员函数: 1) 一个成员变量char* data,表示里面的数据 2) 构造函数MyString(int max_size),将MyString初始化为空串,最大元素个数为max_size 3) 析构函数~MyString(),释放相应的数据 4) Input()函数,往MyString输入数据 5) 重载operator+=()函数,实现两个字符串的连接 6) 重定义Output()和Size()函数 3. 创建DataStructure一个派生类MyStack,该类包括下面成员变量成员函数: 1) 一个成员变量int* data,用于里面的数据 2) 一个成员变量int top,表示最上面的元素下标 3) 构造函数MyStack(int max_size),将MyStack初始化为空栈,最大元素个数为max_size 4) 析构函数~MyStack(),释放相应的数据 5) Push_back(int e)函数,往栈里面压入一个数据e 6) 重定义Output()和Size()函数 4. 编写main函数,测试上面程序的正确性 1) 创建两个MyString的对象str1和str2,分别调用Input函数输入str1和str2,然后分别调用operator+=函数将str2连接到str1的末尾 2) 创建一个MyStack的对象stack,调用Push_back函数输入往stack中输入m(m < max_size)个数据 3) 创建一个长度为3的DataStructure*类型的数组,将其3个元素分别指向str1, str2, stack,然后编写for循环调用Size()和Output()函数输出每个元素的大小和内容。 5. 输入输出样例: 1) 输入样例 A promising techni que for checking reachability 4 12 23 34 45 2) 输出样例 47 A promising technique for checking reachability 29 que for checking reachability 4 12 23 34 45

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值