JAVA之数组详解
1.创建数组:
先声明:
数组元素类型 数组名[ ];
数字元素类型 [ ]数组名;
eg:int arr[ ]或者int [ ]arr;
再用new运算符进行内存分配:
数组名 = new 数组元素类型[数组元素个数];
eg:arr = new int[5];
或者int arr[ ] = new int [5];
再或者int [ ] arr = new int [5];
2.初始化以为数组:
eg:
int arr[ ] = new int [ ] {1, 2, 3, 4};
int arr[ ] = {1, 2, 3, 4};
二维数组创建方法类似:
声明:int arr[ ][ ]或者int [ ][ ]arr;
分配空间:arr = new int [2][3]; 或者arr = new int[2][ ]; arr[0] = new int[3];arr[1] = new int[3];
初始化:int arr[ ][ ] = {{1, 3}, {2, 4}};
3.数组的基本操作:
⑴:遍历数组:可以使用foreach语句,这样更为简便。
eg:
[java]
package Number;
public class IntFunction
{
public static void main (String []args)
{
int b[][] = new int[][]{{1}, {2, 3}, {4, 5, 6}};
for(int i = 0; i < b.length; i ++)
for(int j = 0; j < b[i].length; j++)
System.out.print(b[i][j]);
System.out.println();
for(int x[] : b)
for(int y : x)
System.out.print(y);
System.out.println();
}
}
/*输出结果:
123456
123456
*/
package Number;
public class IntFunction
{
public static void main (String []args)
{
int b[][] = new int[][]{{1}, {2, 3}, {4, 5, 6}};
for(int i = 0; i < b.length; i ++)
for(int j = 0; j < b[i].length; j++)
System.out.print(b[i][j]);
System.out.println();
for(int x[] : b)
for(int y : x)
System.out.print(y);
System.out.println();
}
}
/*输出结果:
123456
123456
*/
⑵.填充替换数组元素:
需要通过Arrays类的静态方法fill()来完成对数组的元素替换。
Arrays.fill(char [ ]a, char c):将数组中所有元素都替换为c字符;
Arrays.fill(char [ ]a, int m, int n, char c):将数组中索引值为m到n之间的字符替换为c字符(不包括索引值为m的元素)
eg:
[java]
package Number;
import java.util.Arrays;
public class IntFunction
{
public static void main (String []args)
{
char ch[] = new char[]{'A', 'S', 'D', 'F'};
Arrays.fill(ch, 'R');
for(char c : ch)
System.out.print(c + " ");
System.out.println();
char ch_1[] = new char[]{'Q', 'W', 'E', 'R', 'T'};
Arrays.fill(ch_1, 2, 4, 'G');
for(char c : ch_1)
System.out.print(c + " ");
System.out.println();
}
}
/*输出结果:
R R R R
Q W G G T
*/
package Number;
import java.util.Arrays;
public class IntFunction
{
public static void main (String []args)
{
char ch[] = new char[]{'A', 'S', 'D', 'F'};
Arrays.fill(ch, 'R');
for(char c : ch)
System.out.print(c + " ");
System.out.println();
char ch_1[] = new char[]{'Q', 'W', 'E', 'R', 'T'};
Arrays.fill(ch_1, 2, 4, 'G');
for(char c : ch_1)
System.out.print(c + " ");
System.out.println();
}
}
/*输出结果:
R R R R
Q W G G T
*/
⑶:对数组进行排序:
Arrays.sort(int [ ]a);
eg:
[java]
?package Number;
import java.util.Arrays;
public class IntFunction
{
public static void main (String []args)
{
int a[] = new int[]{1, 3, 6, 4, 7, 8, 2};
Arrays.sort(a);
for(int x : a)
System.out.print(x);
System.out.println();
}
}
/*输出结果:
1234678
*/
package Number;
import java.util.Arrays;
public class IntFunction
{
public static void main (String []args)
{
int a[] = new int[]{1, 3, 6, 4, 7, 8, 2};
Arrays.sort(a);
for(int x : a)
System.out.print(x);
System.out.println();
}
}
/*输出结果:
1234678
*/
⑷:复制数组:
Arrays.copyOf(int [ ]a, int newlength);
复制数组至指定长度。
newlength:int型常量,指复制后新数组的长度,如果新数组的长度大于数组arr的长度,则需要填充,
根据复制数组的类型确定填充的值,整型数组填充0,字符型数组填充null。
Arrays.copyOfRange(int [ ]a, int m, int n);
将数组的指定长度复制到一个新数组中。(范围为有索引值为m到索引值为n的所有元素,不包括索引值为n的元素)。
eg:
[java]
package Number;
import java.util.Arrays;
public class IntFunction
{
public static void main (String []args)
{
int a[] = new int[]{1, 3, 6, 4, 7, 8, 2};
char c[] = new char[]{'A', 'S', 'D', 'F'};
double d[] = new double[]{1.2, 2.3, 4.3, 5.5};
int a1[] = Arrays.copyOf(a, 10);
int a2[] = Arrays.copyOf(a, 5);
char c1[] = Arrays.copyOf(c, 6);
double d1[] = Arrays.copyOf(d, 6);
for(int x : a1)
补充:软件开发 , Java ,