标签:结果 执行 nbsp 没有 rate system method 字符串数组 abc
定义一个数组:
1 public class Array { 2 public static void main(String[] args) { 3 4 int[] ns = new int[5];//定义一个数组类型的变量,可容纳5个元素 5 ns[0] = 68; 6 ns[1] = 79; 7 ns[2] = 91; 8 ns[3] = 85; 9 ns[4] = 62; 10 } 11 }
数组类型“类型[]”,例如,int[]。和单个基本类型变量不同,数组变量初始化必须使用new int[5]表示创建一个可容纳5个int元素的数组。
ava的数组有几个特点:
0
,浮点型是0.0
,布尔型是false
;
public class Main { public static void main(String[] args) { // 5位同学的成绩: int[] ns; ns = new int[] { 68, 79, 91, 85, 62 }; System.out.println(ns.length); // 5 ns = new int[] { 1, 2, 3 }; System.out.println(ns.length); // 3 } }
对于数组ns
来说,执行ns = new int[] { 68, 79, 91, 85, 62 };
时,它指向一个5个元素的数组:
ns
│
▼
┌───┬───┬───┬───┬───┬───┬───┐
│ │68 │79 │91 │85 │62 │ │
└───┴───┴───┴───┴───┴───┴───┘
执行ns = new int[] { 1, 2, 3 };
时,它指向一个新的3个元素的数组:
ns ──────────────────────┐
│
▼
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│ │68 │79 │91 │85 │62 │ │ 1 │ 2 │ 3 │ │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
但是,原有的5个元素的数组并没有改变,只是指向变了,无法通过变量ns
引用到它们而已。
字符串数组:
public class Arraystring { public static void main(String[] args) { // TODO Auto-generated method stub String[] names = {"abc","zxc","vbn"}; String n = names[1]; names[1] = "cat"; System.out.print(names[1]); } }
这里得到的结果为:cat
参考文章:廖雪峰的官方网站,Java教程
标签:结果 执行 nbsp 没有 rate system method 字符串数组 abc
原文地址:https://www.cnblogs.com/dddtz/p/11594278.html