数组、集合常用方法

1、 java.util.Collection 接口 继承了 java.lang.Iterable 接口
 java.util.List 接口 继承了 java.util.Collection 接口
List<Integer> list = new ArrayList<>();
   2、 list.add( 100 );
3、second.retainAll( first ); // 仅保留 second 那些也包含在 first 的元素 ( 顺我(first)者昌逆我(first)者亡 )
4、second.removeAll( first ); // 移除 second 中那些也包含在 first 中的所有元素
5、String n = list.get( 2 ) ; // 获取 索引 2 对应的元素
5、list.contain(a[i]);//list包含a[i]元素
6、secondList.addAll( firstList ); // 将 firstList 集合中所包含的所有的元素 全部添加到 secondList 集合中
6、Arrays.sort( pandas ); // Arrays.sort( Object[] array )
7、Collections.sort( pandas )//升序
8、Collections.reverse(list);//反转
  Object[] newArr = list.toArray(); //toArray()方法会返回一个包含集合所有元素的Object类型数组
9、System.arraycopy( a , 1 , x , 4 , 3 );// 将 a 数组中 索引 是 1 开始的 3 个元素 复制到 x 数组的 索引为 4 的位置 ( 如果有多个元素,就从 索引 4 开始存放,以此类推 )
10、Arrays.binarySearch(newArr ,2);//使用前先对数组排序,查找2是否包含在数组中,找到返回搜索键的索引;
		

Map<String,Integer> map = new HashMap<>();
8、first.put( "王怡" , 99 );
// 对 可以排序的 集合 ( List ) 进行排序
9、Collections.sort( list );
10、System.out.println( map.get( "王某" ) );
11、System.out.println( map.remove( "王某" ) );
12、map.putAll( first );
13、map.remove( "豆角茄子" );
14、public String trim() 将当前字符串首尾空白剔除后返回剩余的部分(返回新的字符串
15、public String concat( String str ) 将 参数对应的字符串 连接到当前字符串之后并返回新的字符串
16、public static String toString( float value ) 将参数传入的十进制浮点数转换成字符串形式
toString、toHexString、toOctalString、toBinaryString、
17、public static int parseInt( String s ) 将 s 对应的字符串按照十进制解析为 int 类型的数据
18、public static int parseInt( String s , int radix ) 将 s 对应的字符串按照指定的基数( radix )解析为 int 类型的数据
Arrays数组
19、int n=Arrays.binarySearch(newArr ,2);//使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。必须在进行此调用之前对数组进行排序(通过 sort(int[]) 方法)
20、Arrays.sort(array);//根据指定比较器产生的顺序对指定对象数组进行排序。升序
21、y.append(x)//在y后面追加x
Set<String> set=new HashSet<>();
1、set.add(i);//向set集合中添加不重复的元素
String s="I am working i am student";
String[] array=s.split("\\s");//将字符串以空格截取单个字符串存进array数组里,字符若字符串第一个为空格则算一个字符串。

    1、  for( int i = 0 , n = list.size() ; i < n ; i++ ) {
			String name = list.get( i ) ; // 从 list 集合中获取 索引 i 对应的元素
			System.out.println( i + " , " +  name );
		}

2、 for (  类型  变量 :  Iterable 实例 或 数组 ) {
		 *  
		 *  }
// "foreach" 循环
		for( String name  : list ) {
			System.out.println( name );
		}
 3、   迭代 Map 集合的方法一: 使用 键集 ( 所有的 键 组成的 集合 )   (重要程度:  * * * * *  )  <br> 
// 获得 某个 map 实例中 所有的 key 组成的 集合 ( Map 集合中不能包含重复的键 ,但可以包含重复的值 )
		Set<String> keys = map.keySet(); // keys 变量 的类型是 Set 类型,Set 继承了 Collection
		
		// 获得 用来迭代 所有 key 组成的集合的 迭代器
		Iterator<String> itor = keys.iterator();
		
		// 使用迭代器 迭代 键集 ( 所有 的 key 组成的集合 )
		while( itor.hasNext() ) { // 判断 是否存在 下一个 元素
			String key = itor.next(); // 获得下一个 元素 
			Integer value = map.get( key ); // 根据 key 获得 value 
			System.out.println( key  +  " : " + value );
		}
4、迭代 Map 集合的方法三: 使用 键值对集 ( 所有的 键值对(映射项) 组成的 集合 )   (重要程度:  * * * * *  )  <br>
Set< Map.Entry<String,Integer> > es = map.entrySet(); // 返回 map 集合内部 所有的 映射项( Entry ) 组成的集合 ( 就是 键-值对 组成的集合 )
		
		// 获得用来 迭代 entrySet 集合 的 迭代器
		Iterator< Map.Entry<String,Integer> > itor =  es.iterator() ; 
		
		// 使用迭代器 ( itor ) 迭代 entrySet 集合 ( es )
		while( itor.hasNext() ) {
			Map.Entry<String, Integer> entry = itor.next();
			System.out.println( entry.getKey() + " : " + entry.getValue() );
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值