Ordinary arrays
package com.cnblogs.www.demo9;
public class ArrayTest {
public static void main(String[] args) {
int [] = {1,2,3,4,5} ARR; // declare ordinary array, containing elements of the same type and number of a fixed value
System.out.println ( "for loop traversal array:" ) ;
for ( int I = 0; I <. 5; I ++ ) {
System.out.println(arr[i]+" ");
}
System.out.println ( "===================== separate line ==================== = " );
Of System.out.print ( "use of only one property to obtain the length of the array:" );
System.out.println(arr.length);
System.out.print ( "using an index of elements in the array, query the index for the element 1:" );
System.out.println(arr[1]);
System.out.print ( "using an index of elements in the array to be modified, modify the index to the elements 1 and query:" );
arr[1] = 7;
System.out.println(arr[1]);
}
}
Dynamic Array
package com.cnblogs.www.demo9;
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
ARR ArrayList = new new ArrayList (); // declare ArrayList array containing the elements is not fixed, the same type
arr.add ( "reading"); // add elements to the array
arr.add ( "riding" );
arr.add ( "Battle Flag" );
arr.add ( "anime" );
Of System.out.print ( "Use size () method to obtain an array of length:" );
System.out.println(arr.size());
System.out.println ( "used for loop traverse the array, GET () method to get the values of the array, the parameter is I:" );
for ( int I = 0; I <arr.size (); I ++ ) {
System.out.println(arr.get(i));
}
System.out.println ( "============================= separate line ============ ===================== " );
System.out.println ( "delete the index for the element 1:" );
arr.remove(1);
System.out.println ( "to traverse the array, the index can be seen that the element 1 has been deleted:" );
for ( int I = 0; I <arr.size (); I ++ ) {
System.out.println(arr.get(i));
}
System.out.println ( "============================== separate line =========== ===================== " );
System.out.println ( "using the index for the query" );
System.out.println(arr.get(0));
System.out.println (arr.get ( 1)); // When you delete an element, the next element who will occupy the element to be removed index
System.out.println (arr.get (2 ));
// System .out.println (arr.get (3)); // query the index for the element 3, showing the array subscript bounds exception, because there is no.
System.out.println ( "to traverse the array, the array can be seen that the case of only three elements, and the index starts from 0:" );
for ( int I = 0; I <arr.size (); I ++ ) {
System.out.println(arr.get(i));
}
System.out.println ( "================================ separate line ========= ===================== " );
arr.add ( "food"); // Although there is no error, but there is a type-safe warning. Tips should use generics. Generic next exercise summary. Note: The warning can be turned off, I do not choose to close for the convenience of self-study.
System.out.println ( "to traverse the array, it can be seen in the last element added to the array of food:" );
for ( int I = 0; I <arr.size (); I ++ ) {
System.out.println(arr.get(i));
}
}
}
Generics
package com.cnblogs.www.demo9;
import java.util.ArrayList;
public class GenericityTest {
public static void main(String[] args) {
ArrayList<Object> arr = new ArrayList<Object>(); //可以存储不同类型的元素
arr.add(1); //jdk1.5版本新功能,自动装箱,将基本数据类型自动封装,在没有类型匹配时。
arr.add("战旗");
arr.add(2);
arr.add("读书");
arr.add(3);
arr.add("动漫");
System.out.print("使用size()获得长度:");
System.out.println(arr.size());
System.out.println("使用for循环遍历,get()方法获得数组内的元素:");
for(int i = 0; i < arr.size(); i++) {
System.out.print(arr.get(i) + " ");
}
System.out.println(" ");
arr.add("美食"); //字符串,此时使用add()方法添加元素时不会出现黄色的警告提示了
arr.add(4); //整数
arr.add(2.5); //浮点数
arr.add(-2.5); //浮点数
arr.add('学'); //字符
arr.add(true); //布尔
arr.add(false); //布尔
System.out.println("使用for循环遍历,可以看到不同类型的元素也能正常添加:");
for(int i = 0; i < arr.size(); i++) {
System.out.print(arr.get(i) +" ");
}
}
}
遍历数组时,有一个问题,那就是使用输出语句时利用了空格来完成元素的间隔。
如果空格换成顿号,就规范多了,可是这样新的问题又出现了,那就是最后一个顿号也会显示。
这里我尝试了下使用if判断语句来解决这个问题
package com.cnblogs.www.demo9;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Object> arr = new ArrayList<Object>();
arr.add("读书");
arr.add("骑行");
arr.add("战旗");
System.out.println("使用for循环遍历");
for(int i = 0; i < arr.size(); i++) {
System.out.print(arr.get(i));
//==(关系运算符)可以用来比较值,这里的值:i是遍历的次数,arr.size()-1是最后一个索引值。
if(i == arr.size()-1){
System.out.print(" ");
}else{
System.out.print("、");
}
}
}
}