Java二维数组

package Day5;

public class Num31 {
    public static void main(String[] args) {
  /*二维数组:
        1.定义一个int类型数组a,数组长度是4
          a中第一个元素有5个元素
          a中第二个元素有3个元素
          a中第3个元素有6个元素,其中第三个是2
        2.定义一个int类型数组,数组长度是3
          b中每一个元素都有4个元素
          b中第2个元素的第4个元素是5
          b中的每一个元素都是5  */
        int[][] a=new int[4][];
        a[0]=new int[5];
        a[1]=new int[3];
        a[2]=new int[6];
        a[2][2]=2;
        int[][] b=new int[3][4];
        /*for (int i=0;i<b.length;i++){
            b[i]=new int[5]; */
        b[1][3]=5;
        for(int i=0;i<b.length;i++){
            for(int j=0;j<b[i].length;j++){
                b[i][j]=5;
                System.out.println(b[i][j]);
            }
        }
    }
}

```java
package Day5;

import java.util.Arrays;

public class Num3 {
    public static void main(String[] args) {
        int[] a={1,2,13,14,15,16,14};
        a=Arrays.copyOf(a,a.length+2);
        a[a.length-2]=100;
        a[a.length-1]=200;
        System.out.println(Arrays.toString(a));

        int[] b=new int[a.length+3];
        System.arraycopy(a,0,b,0,3);
        b[b.length-3]=100;
        b[b.length-2]=200;
        b[b.length-1]=300;
        System.out.println(Arrays.toString(b));

    }
}
public class Num2 {
    public static void main(String[] args) {
        int[] a = {2, 5, 8, 9, 6};
        int[] b = new int[a.length + 1];
        //System的用法一:arraycopy();--->数组扩容
        /*System.arraycopy(参数1,参数2,参数3,参数4,参数5);
            参数1: 要复制的数组 - 源数组
            参数2: 从要源数组的哪个位置开始复制
            参数3: 要复制到哪个数组中 - 目标数组
            参数4: 要从b数组的哪个位置开始存放值
            参数5: 要复制几个元素
        */
        b[b.length - 1] = 8;
        System.arraycopy(a, 0, b, 0, 5);
        System.out.println(Arrays.toString(a));

        //System的用法一:copyOf();--->数组扩容
        /*Arrays.copyOf(参数1,参数2,返回值);
            参数1: 要复制的数组 -> 源数组
            参数2: 要生成的新数组的长度
            返回值: 生成的新数组
         */
        a = Arrays.copyOf(a, a.length + 1);
        a[a.length - 1] = 9;
        System.out.println(Arrays.toString(a));

        /*
        long time=System.currentTimeMillis();
        String a="";
        System.out.println(a);

        */
    }
}



```java
 String字符串:
 构造方法:
        String s = "hello";
        String()
        String(String)
        String(char[])
        String(char[], int offset, int count)
        String(byte[])
        String(byte[], int offset, int length)
 
public class Num02 {
    public static void main(String[] args) {
        //1.简单写法
        String s="hello";
        //2.无参构造器--->""空的字符串
        String s1=new String();
        //3.有参构造器1
        String s2=new String("hello");
        //4.有参构造器2
        char[] chs={'h','e','l','l','o'};
        String s3=new String(chs);
        //5.有参构造器3 输出ello
        String s4=new String(chs,1,4);
        //6.有参构造器4 输出ABCDa  //byte可以转换成char类型
        byte[] bs={65,66,67,97};//或byte[] bs={65,66,67,97};
        String s5=new String(bs);
        //7.有参构造5 输出ABC
        String s6=new String(bs,0,3);
        System.out.println(s5);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值