Java入门:ArrayList<Integer>的14个基本使用

import java.util.*;
class arraylisttest{
    public static void main(String[] args){

1.添加元素

//添加
ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(12);
list2.add(23);
list2.add(9);

2.最大最小值

// get max,min
int maxa = Collections.max(list2),mina = Collections.min(list2);
System.out.println("list2's max value is " + maxa);
System.out.println("list2's min value is " + mina);
list2's max value is 23
list2's min value is 9

3.输出

//print 
System.out.println("whole list2 is "+list2);
whole list2 is [12, 23, 9]

4.拷贝

//拷贝
ArrayList<Integer> list21 = new ArrayList<Integer>();
list21.addAll(list2);
System.out.println("list21:copy list2 is "+list21);//不变
list21:copy list2 is [12, 23, 9]

5.从小到大排序

Collections.sort(list2);
System.out.println("list2 sort is "+list2);
list2 sort is [9, 12, 23]

6.从大到小排序

Collections.sort(list2,Collections.reverseOrder());
System.out.println("list2 descend sort is "+list2);
list2 descend sort is [23, 12, 9]

7.倒序

Collections.reverse(list21);
System.out.println("reverse list21 is "+list21);
reverse list21 is [9, 23, 12]

8.求某个元素首次出现的位置

int idx = list2.indexOf(23);
System.out.println("list2 is "+list2+" and 23 locates :"+idx);
list2 is [23, 12, 9] and 23 locates :0

9.求和

int sum = 0;
for (int i=0;i<list2.size();i++){
    sum += list2.get(i);
}
System.out.println("list2 sum is "+sum);


//利用stream求和
List<Integer> ls = new ArrayList<Integer>();
ls.add(43);
ls.add(27);
int sum2 = ls.stream().mapToInt(a->a).sum();
System.out.println("sum is "+sum2);
list2 sum is 44
sum is 70

10.在指定位置插入元素

//在1位置插入一个88(like insert)
list2.add(1,88);
System.out.println("insert 88 at [1] and list2 is "+list2);
insert 88 at [1] and list2 is [23, 88, 12, 9]

11.移除某个位置的元素

list2.remove(1);
System.out.println("remove [1] and list2 is "+list2);
list2.remove(list2.size()-1);//删除末尾
System.out.println("remove the last and list2 is "+list2);
remove [1] and list2 is [23, 12, 9]
remove the last and list2 is [23, 12]

12.移除某个值

list2.add(0,23);
System.out.println("add 23 at [0] and list2 now is "+list2);
for (int i=0;i<list2.size();i++){
    if (list2.get(i)==23){
        list2.remove(i);
        i--;
    }
}
System.out.println("remove 23 and list2 is "+list2);
remove 23 and list2 is [12]

13.切片

ArrayList<Integer> list3 = new ArrayList<Integer>();
list3.add(45);
list3.add(52);
list3.add(66);
System.out.println("list3 is "+list3);
System.out.println("list3[0:2] is "+list3.subList(0,2));
list3 is [45, 52, 66]
list3[0:2] is [45, 52]

14.转化为整形数组

import java.util.*; 
class Solution {
    public int[] twoSum(int[] nums, int target) {
        ArrayList<Integer> re = new ArrayList<Integer>();
        for (int i=0;i<nums.length;i++){
            for (int j=i+1;j<nums.length;j++){
                if (nums[i]+nums[j]==target){
                    re.add(i);
                    re.add(j);
                }
            }
        }
        int[] res = re.stream().mapToInt(Integer::valueOf).toArray();
        return res;
    }
    
    
    
    // 调用
    public static void main(String[] args){
        int[] nums = {2,7,11,9};
        int target = 9;
        Solution s = new Solution();
        int[] lst = s.twoSum(nums,target);
        System.out.println(Arrays.toString(lst));
    }
}
[0, 1]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值