首页 > 代码库 > Java_1_初识_基本数据类型与数组

Java_1_初识_基本数据类型与数组

Java的基本数据类型按精度级别由低到高:

byte  short  char  int  long  float  double

实验一:

数据之间的转换:char和int(输出希腊字母表)

public class Program2_1 {
public static void main(String args[]){
int startPosition = 0, endPosition = 0;
char cStart = ‘α‘, cEnd = ‘ω‘;
startPosition = (int)cStart;
endPosition = (int)cEnd;
System.out.println("希腊字母\‘α"+"\‘在unicode表中的顺序位置:"+startPosition);
System.out.println("希腊字母表:");
for(int i = startPosition; i<= endPosition; i++){
char c = ‘\0‘;
c = (char)i;
System.out.println(""+c);
if((i-startPosition+1)%10==0)
System.out.println("");
}
}
}

举一反一:

public class Program2_test1 {
public static void main(String args[]) {
int startPosition = 0, endPosition = 0;
char cStart = ‘A‘, cEnd = ‘Z‘;
startPosition = (int)cStart;
endPosition = (int)cEnd;
System.out.println("英文字母\‘A\‘在unicode中的位置是:"+startPosition);
System.out.println("英文字母表如下:");
for(int i = startPosition; i <= endPosition; i++){
char c = ‘\0‘;
c = (char)i;
System.out.println(""+c);
}
}
}

实验二:

数组的引用与元素:

public class program2_2 {
public static void main (String args[]){
int [] a = {100,200,300};
System.out.println(a.length);
System.out.println(a);
int b[][] = {{1},{1,1},{1,2,1},{1,3,3,1},{1,4,6,4,1}};
System.out.println(b.length);
System.out.println(b[4][2]);
b[4] = a;
System.out.println(b[4][2]);
}
}

相关知识点:

数组属于引用型变量,对于

int a[] = {1,2,3},b [] = {4,5};

数组变量a和b分别存放着引用,a的值是0x35ce36,b的值是0x757aef;

对于一维数组,“数组名.length”的值就是数组中元素的个数,对于二维数组,“数组名.length”的值是他含有的一维数组的个数。

实验三:

便利与复制数组:

import java.util.Arrays;
public class Program2_3 {
public static void main(String args[]){
int [] a = {1,2,3,4,500,600,700,800};
int [] b,c,d;
System.out.println(Arrays.toString(a));
b = Arrays.copyOf(a,a.length);
System.out.println(Arrays.toString(b));
c = Arrays.copyOf(a,4);
System.out.println(Arrays.toString(c));
d = Arrays.copyOfRange(a,4,a.length);
System.out.println(Arrays.toString(d));
c[c.length-1] = -100;
d[d.length-1] = -200;
System.out.println(Arrays.toString(a));
}
}

举一反一:

import java.util.Arrays;
public class Program2_test3 {
public static void main(String args[]){
int [] a = {1,2,3,4,5,100,200,300,400,500};
int [] b,c,d;
b = Arrays.copyOf(a, a.length);
System.out.println(Arrays.toString(b));
c = Arrays.copyOf(a,5);
System.out.println(Arrays.toString(c));
d = Arrays.copyOfRange(a,5,a.length);
System.out.println(Arrays.toString(d));

}
}

Java_1_初识_基本数据类型与数组