细节
请注意,对于`float`类型,当在代码中直接赋值时,需要添加`f`或`F`后缀来表示`float`类型;对于`long`类型,当在代码中直接赋值时,需要添加`l`或`L`后缀来表示`long`类型;在数组初始化时,如果你写的数字在`int`类型的范围内,Java会自动将它们视为`int`类型,除非你明确地使用后缀;对于`double`类型,则不需要后缀,因为 Java 默认将不带后缀的小数视为`double`类型。
1. byte 数组
byte[] byteArray = {1, 2, 3, 4, 5};
byte[] byteArray2 = new byte[5];
byteArray2[0] = 1;
byteArray2[1] = 2;
2. short 数组
short[] shortArray = {10, 20, 30, 40, 50}; short[] shortArray2 = new short[5];
shortArray2[0] = 10;
shortArray2[1] = 20;
3. int 数组
int[] intArray = {100, 200, 300, 400, 500}; int[] intArray2 = new int[5];
intArray2[0] = 100;
intArray2[1] = 200;
4. long 数组
long[] longArray = {1000L, 2000L, 3000L, 4000L, 5000L}; long[] longArray2 = new long[5];
longArray2[0] = 1000L;
longArray2[1] = 2000L;
5. float 数组
float[] floatArray = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f}; float[] floatArray2 = new float[5];
floatArray2[0] = 1.1f;
floatArray2[1] = 2.2f;
6. double 数组
double[] doubleArray = {1.11, 2.22, 3.33, 4.44, 5.55}; double[] doubleArray2 = new double[5];
doubleArray2[0] = 1.11;
doubleArray2[1] = 2.22;
7. boolean 数组
boolean[] booleanArray = {true, false, true, false, true}; boolean[] booleanArray2 = new boolean[5];
booleanArray2[0] = true;
booleanArray2[1] = false;
8. char 数组
char[] charArray = {'a', 'b', 'c', 'd', 'e'}; char[] charArray2 = new char[5];
charArray2[0] = 'a';
charArray2[1] = 'b';
9. String 数组
1、String[] stringArray = {"apple", "banana", "cherry", "date", "elderberry"}; 2、String[] stringArray2 = new String[5]; stringArray2[0] = "fig"; stringArray2[1] = "grape"; stringArray2[2] = "honeydew"; stringArray2[3] = "indian plum"; stringArray2[4] = "jackfruit";3、int length = 10; String[] stringArray3 = new String[length];for (int i = 0; i < length; i++) {stringArray3[i] = "Fruit" + (i + 1); }