1 数组定义
数组的概念
是一种容器,可以同时存放多个数据值。
数组的特点
- 数组是一种引用数据类型
- 数组当中的多个数据,类型必须统一
- 数组的长度在程序运行期间不可改变
2 定义格式化
2.1 动态格式化(指定长度)
格式:数据类型[ ] 数组名称 = new 数据类型[数组长度];
解析含义:左侧数据类型也就是数组当中保存的数据,全都是统一的什么类型。
左侧的中括号:代表我是一个数组。
左侧数组名称:给数组取一个名字。
右侧的new:代表创建数组的动作。。
右侧数据类型:必须和左边的数据类型保持一致。
右侧中括号的长度:也就是数组当中,到底可以保存多少个数据,是一个int数字。
public static void main(String[] args) {
int[] arrayA = new int[300];
double[] arrayB = new double[10];
String[] arrayC = new String[5];
}
2.2 静态初始化
在创建数组的时候,不直接指定数据个数多少,而是直接将具体的数据内容进行指定。
静态初始化基本/标准格式:
数据类型[ ] 数组名称 = new 数据类型[ ] {元素1,元素2,…}
省略格式:
数据类型[ ] 数组名称 = {元素1,元素2,…}
注意事项:
- 虽然静态初始化没有直接告诉长度,但是根据大括号里面的元素具体内容,也可以自动推算出长度。
- 静态初始化标准格式可以拆分为两个步骤。
- 动态初始化也可以拆分为两个步骤。
- 静态初式化一旦使用省略格式,就不能拆分为两个步骤。
使用建议:
如果不确定数组当中的具体内容,用动态是实话,否则,已经确定了具体的内容,用静态初始化。
public static void main(String[] args) {
int[] arrayA = {5, 15, 25};
int[] arrayB;
arrayB = new int[]{1, 2, 3};
int[] arrayC;
arrayC = new int[5];
}
3 访问数组元素进行 获取
直接打印数组名称,得到的是数组对应的: 内存地址哈希值
访问数组元素的格式:数组名称[索引值]
索引值:就是一个int数字,代表数组当中元素的编号。
【注意】索引值从0开始,一直到“数组长度为-1”为止。
public static void main(String[] args) {
int[] array = {10, 20, 30};
System.out.println(array);
System.out.println(array[0]);
int num = array[1];
System.out.println(num);
}
4 访问数组元素进行 赋值
使用动态初始化数组的时候,其中元素将会自动拥有一个默认值。规则如下:
<h2><a id="1__0"></a>1 数组定义</h2>
<p><code>数组的概念</code><br />
是一种容器,可以同时存放多个数据值。<br />
<code>数组的特点</code></p>
<ol>
<li>数组是一种引用数据类型</li>
<li>数组当中的多个数据,类型必须统一</li>
<li>数组的长度在程序运行期间不可改变</li>
</ol>
<h2><a id="2__8"></a>2 定义格式化</h2>
<h3><a id="21__9"></a>2.1 动态格式化(指定长度)</h3>
<p>格式:数据类型[ ] 数组名称 = new 数据类型[数组长度];<br />
解析含义:左侧数据类型也就是数组当中保存的数据,全都是统一的什么类型。<br />
左侧的中括号:代表我是一个数组。<br />
左侧数组名称:给数组取一个名字。<br />
右侧的new:代表创建数组的动作。。<br />
右侧数据类型:必须和左边的数据类型保持一致。<br />
右侧中括号的长度:也就是数组当中,到底可以保存多少个数据,是一个int数字。</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-comment">// 创建一个数组,里面可以存放300个int数据</span>
<span class="hljs-comment">// 格式:数据类型[] 数组名称 = new 数据类型[数组长度];</span>
<span class="hljs-type">int</span>[] arrayA = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[<span class="hljs-number">300</span>];
<span class="hljs-comment">// 创建一个数组,能存放10个double类型的数据</span>
<span class="hljs-type">double</span>[] arrayB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">double</span>[<span class="hljs-number">10</span>];
<span class="hljs-comment">// 创建一个数组,能存放5个字符串</span>
String[] arrayC = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>[<span class="hljs-number">5</span>];
}
</code></div></pre>
<h3><a id="22__32"></a>2.2 静态初始化</h3>
<p>在创建数组的时候,不直接指定数据个数多少,而是直接将具体的数据内容进行指定。</p>
<p>静态初始化基本/标准格式:<br />
数据类型[ ] 数组名称 = new 数据类型[ ] {元素1,元素2,…}</p>
<p>省略格式:<br />
数据类型[ ] 数组名称 = {元素1,元素2,…}</p>
<p>注意事项:</p>
<ol>
<li>虽然静态初始化没有直接告诉长度,但是根据大括号里面的元素具体内容,也可以自动推算出长度。</li>
<li>静态初始化标准格式可以拆分为两个步骤。</li>
<li>动态初始化也可以拆分为两个步骤。</li>
<li>静态初式化一旦使用省略格式,就不能拆分为两个步骤。</li>
</ol>
<p>使用建议:<br />
如果不确定数组当中的具体内容,用动态是实话,否则,已经确定了具体的内容,用静态初始化。</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-comment">// 直接创建一个数组,里面装的全都是int数字,具体为:5,15,25</span>
<span class="hljs-type">int</span>[] arrayA = {<span class="hljs-number">5</span>, <span class="hljs-number">15</span>, <span class="hljs-number">25</span>};
<span class="hljs-comment">// 静态初始化的标准格式,可以拆分为两个步骤</span>
<span class="hljs-type">int</span>[] arrayB;
arrayB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[]{<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>};
<span class="hljs-comment">// 动态初始化也可以拆分为两个步骤</span>
<span class="hljs-type">int</span>[] arrayC;
arrayC = <span class="hljs-keyword">new</span> <span class="hljs-title class_">int</span>[<span class="hljs-number">5</span>];
<span class="hljs-comment">// 静态格式化一旦使用省略格式,就不能拆分为两个步骤</span>
<span class="hljs-comment">// int[] arrayD;</span>
<span class="hljs-comment">// arrayD = {1, 2, 3};</span>
}
</code></div></pre>
<h2><a id="3___69"></a>3 访问数组元素进行 获取</h2>
<p>直接打印数组名称,得到的是数组对应的: 内存地址哈希值</p>
<p>访问数组元素的格式:数组名称[索引值]<br />
索引值:就是一个int数字,代表数组当中元素的编号。<br />
【注意】索引值从0开始,一直到“数组长度为-1”为止。</p>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
<span class="hljs-comment">// 静态初始化的省略格式</span>
<span class="hljs-type">int</span>[] array = {<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>};
<span class="hljs-comment">// 直接打印数组当中的元素</span>
System.out.println(array); <span class="hljs-comment">// [I@1b6d3586</span>
System.out.println(array[<span class="hljs-number">0</span>]); <span class="hljs-comment">// 10</span>
<span class="hljs-comment">// 也可以将数组当中的某一个单个元素,赋值交给变量</span>
<span class="hljs-type">int</span> <span class="hljs-variable">num</span> <span class="hljs-operator">=</span> array[<span class="hljs-number">1</span>];
System.out.println(num);
}
</code></div></pre>
<h2><a id="4___91"></a>4 访问数组元素进行 赋值</h2>
<p>使用动态初始化数组的时候,其中元素将会自动拥有一个默认值。规则如下:</p>
留言