java array declaration _java array definition common methods

Java array definition common methods

Arrays in Java are a simple linear data storage structure. They sacrifice the size of automatic expansion in exchange for the only advantage compared with collections - the improvement of query efficiency. What are the types of arrays in Java? How do we define these arrays? Let's learn the common methods of Java array definition with yjbys editor!

There are two data types in java:

a) reference type

b) base type

There are two basic types:

b1) Numerical type

b2) and Boolean type.

Array - also a data type of java, classified as a reference type. This article intends to make two points clear:

1. Array declaration and initialization.

2. Commonly used array methods.

One additional point: For the two-dimensional arrays and multi-dimensional arrays that we often say, they are actually an extension of one-dimensional arrays. Here we only focus on one-dimensional arrays for the time being.

[Array declaration and initialization]

1. Array declaration:

As a reference type, as we usually declare when using reference types, there are generally two ways to write:

a) type[] arrayName; exp: String[] strArray;

b) type arrayName[]; exp: String strArray[];

The second way of writing is derived from C, because it is easy to cause confusion, so this declaration method is basically not used now.

2. Initialization of the array:

There are two types of array initialization:

a) Static initialization - the size of the array is allocated by the system, we only assign values ​​to each position of the array

String[] strArray1 = {"a", "b", "c", "d", "e"};

String[] strArray2 = new String[]{"a", "b", "c", "d", "e"};//The size of String array cannot be specified in new String[]!

b) Dynamic initialization - only specify the size of the value, the initialization work is done by the system for us (that is, assign an initial value to each position of the array)

String[] strArray3 = new String[5];//此时String数组的每个位置上的值都由系统来初始化、使用默认的值""

//我们能做的是动态的为strArray3每个位置上的值进行修改

for (int i = 0; i < strArray1.length; i++) {

//这里仅用原始的方法进行赋值。

strArray3[i] = strArray1[i];

}

【数组的常用方法】

package com.chy.array.usefulMethods;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.HashSet;

import java.util.Set;

import com.chy.array.bean.Student;

@SuppressWarnings("all")

public class ArrayUseFulMethoed {

private static int[] intArray = {1, 2, 3, 4, 5};

private static String[] strArray = {"a", "b", "c", "d", "e"};

/**

* 填充元素、比较大小、复制元素

*/

public static void testFillArray(){

//注意字符串和对象的不同

Student[] student1 = new Student[4];

Student[] student2 = new Student[4];

System.out.println(Arrays.equals(student1, student2));//true

Arrays.fill(student1, 0, 4, new Student(1,"chy"));

Arrays.fill(student2, new Student(1,"chy"));

System.out.println(Arrays.equals(student1, student2));//false

String[] str1 = new String[4];

String[] str2 = new String[]{"a", "a", "a", "a"};

String[] str3 = {new String("a"), new String("a"), new String("a"), new String("a")};

Arrays.fill(str1, "a");

System.out.println(Arrays.equals(str1, str2));//true

System.out.println(Arrays.equals(str2, str3));//true

String[] str4 = Arrays.copyOf(str1, 2);//是将传入的数组拷贝len个元素到新的数组、相当于复制本身的一部分或者全部形成一个全新的数组

System.out.println(str4.length + "=======" + Arrays.toString(str4));// 2=======[a, a]

String[] str5 = new String[8];

System.arraycopy(str4, 0, str5, 6, 2);//是将str4从下标0开的2个元素拷贝到从下标6开始放置的数组str5中

System.out.println(str5.length + "=======" + Arrays.toString(str5));// 8=======[null, null, null, null, null, null, a, a]

}

/**

* 以字符串的形式输出指定数组的“模样”

*/

public static void printOriginalArray(){

String intArrayToString = Arrays.toString(intArray);

System.out.println(intArrayToString); //result: [1, 2, 3, 4, 5]

}

/**

* 将数组转化成List集合

* 注意:不能直接将int[]转化为集合、因为asList()方法的参数必须是对象。应该先把int[]转化为Integer[]。

* 对于其他primitive类型的数组也是如此,必须先转换成相应的wrapper类型数组。

*/

public static void convetArrayToList(){

Integer[] integerArray = new Integer[intArray.length];

for (int i = 0; i < integerArray.length; i++) {

integerArray[i] = intArray[i];

}

ArrayList integerList1 = new ArrayList(Arrays.asList(integerArray));

/*

* 不能写成下面:

* ArrayList integerList2 = (ArrayList)Arrays.asList(integerArray);

* 返回的是List、强转可以通过编译、但是不能正常使用。

*/

for(int i : integerList1){

System.out.print(i);

}

//result: 12345

System.out.println();

}

/**

* 将List集合转换成数组

*/

public static void convetListToArray(){

ArrayList strList = new ArrayList(Arrays.asList(strArray));

String[] strArrayFromList = new String[strList.size()];

strList.toArray(strArrayFromList);

System.out.println(Arrays.toString(strArrayFromList)); //result: [a, b, c, d, e]

/*

* 注意:不能写成这样:String[] strArrayFromList = (String[])strList.toArray(strArrayFromList);会抛出ClassCastException。

* List.toArray()与List.toArray(T[] t)的区别在于:

* List.toArray()返回的是一个Object[]、不能强转成String[]、强转的话可以通过编译、但是不能进行String[]的操作

* 而List.toArray(T[] t)会将list的值转换成T类型的数组。

*/

}

/**

* 将数组转换成Set集合

*/

public static void convertArrayToSet(){

Set set = new HashSet(Arrays.asList(strArray));

//Set具有无序性、所以输出结构不一定是原来数组元素存放顺序

System.out.println(set); //result: [d, e, b, c, a]

}

/**

* 判断某个数组中是否包含一个元素、思路:将数组转换成list使用list的contains方法

*/

public static void isContainObject(){

ArrayList strList = new ArrayList(Arrays.asList(strArray));

System.out.println(strList.contains("a")); //result: true

//另一种实现

Arrays.sort(strArray);

if(Arrays.binarySearch(strArray, "c") >= 0){

System.out.println(true);

}else{

System.out.println(false);

}

}

/**

* 将两个相同类型的数组连接起来

*/

public static void connTwoSameArray(){

int[] intArray2 = new int[]{6, 7, 8, 9, 10};

}

/**

* 将数组中数据排序

*/

public static void sortArray(){

String[] str = {"c", "a" ,"d" ,"z" };

Arrays.sort(str);

System.out.println(Arrays.toString(str));

//反序、

Arrays.sort(str, Collections.reverseOrder());

System.out.println(Arrays.toString(str));

}

public static void main(String[] args) {

/*printOriginalArray();

convetArrayToList();

convetListToArray();

isContainObject();

convertArrayToSet();

sortArray();*/

testFillArray();

}

}

【Java数组定义常用方法】相关文章:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324033783&siteId=291194637