Java数组基础练习,以及一些方法使用

Java数组基础练习,以及一些方法使用

package com.baishi;

import java.util.Arrays;

public class ArrTest {
    public static void main(String[] args) {
        // 1、声明数组
        // 声明数组之后,长度不可更改
        System.out.println("1、声明数组");

        int[] arr1 = new int[3];
        // 打印数组地址
        System.out.println(arr1);
        for(int i1 = 0 ;i1<arr1.length; i1++){
            System.out.println(arr1[i1]);
        }
        System.out.println("----------------------");

        // 2、连接两个数组
        System.out.println("2、连接两个数组");
        int[] arr2 = {2,3,4};
        int[] combined = new int[arr1.length+arr2.length];
        /*
        src是源数组
        srcPos是源数组复制的起始位置
        dest是目标数组
        destPos是目标数组接收复制数据的起始位置
        length是复制的长度(源数组中从复制起始位置srcPos开始需要复制的长度)
        */
        System.arraycopy(arr1, 0, combined, 0, arr1.length);  // 复制arr1数组进 combined
        System.arraycopy(arr2, 0, combined, arr1.length, arr2.length); //复制arr2数组进 combined
        for(int i1 = 0 ;i1<combined.length; i1++){
            System.out.println(combined[i1]);
        }

        System.out.println("----------------------");

        // 3、判断数组内部是否包含某个值
        /*
        小坑记录:

        使用int类型的时候,aslist这个方法会整体的将这个int数组作为一个对象编程一个list,不是把里面的元素作为对象!
        主要是这个aslist这个方法的参数再起作用,这个参数必须传进来能够类型范化的参数!
        */

        //将 combined 数组的 int[] 类型转换为 Integer 类型
        Integer[] interger_combined2 = new Integer[combined.length];
        for(int i =0; i<combined.length; i++) {
            interger_combined2[i] = combined[i];
        }

        boolean b = Arrays.asList(interger_combined2).contains(3);
        if(b) {
            System.out.println("if 判断正确");
            System.out.println(true);
        } else {
            System.out.println("if 判断错误");
            System.out.println(false);
        }
        System.out.println("----------------------");


    }
}

运行结果截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值