实验四 4-2 数组和字符串
建立使用数组的程序,本程序建立了一个长度为 5 的 1 维数组,一个长度为 12 的 2 维数组
编译结果为:a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
arr1(0,0) = 0
arr1(0,1) = 1
arr1(0,2) = 2
arr1(1,0) = 3
arr1(1,1) = 4
arr1(1,2) = 5
Process completed.
2).改进程序源代码:
public class KY5_1 {
public static void main(String args[]) {
int a[]=new int[5];
int arr1[][]=new int[3][4];
a[0]=10;
a[1]=10+a[0];
a[2]=30;
a[3]=40;
a[4]= a[1]+ a[2];
arr1[0][0]=0; arr1[0][1]=1; arr1[0][2]=2;
arr1[1][0]=3; arr1[1][1]=4; arr1[1][2]=5;
arr1[2][0]=6; arr1[2][1]=7; arr1[2][2]=8;
System.out.println("a["+0+"] = "+a[0]);
System.out.println("a["+1+"] = "+a[1]);
System.out.println("a["+2+"] = "+a[2]);
System.out.println("a["+3+"] = "+a[3]);
System.out.println("a["+4+"] = "+a[4]);
System.out.println("arr1("+0+","+0+") = "+arr1[0][0]);
System.out.println("arr1("+0+","+1+") = "+arr1[0][1]);
System.out.println("arr1("+0+","+2+") = "+arr1[0][2]);
System.out.println("arr1("+1+","+0+") = "+arr1[1][0]);
System.out.println("arr1("+1+","+1+") = "+arr1[1][1]);
System.out.println("arr1("+1+","+2+") = "+arr1[1][2]);
System.out.println("arr1("+2+","+0+") = "+arr1[2][0]);
System.out.println("arr1("+2+","+1+") = "+arr1[2][1]);
System.out.println("arr1("+2+","+2+") = "+arr1[2][2]);
}
}
3)改进后程序的编译结果:
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
arr1(0,0) = 0
arr1(0,1) = 1
arr1(0,2) = 2
arr1(1,0) = 3
arr1(1,1) = 4
arr1(1,2) = 5
arr1(2,0) = 6
arr1(2,1) = 7
arr1(2,2) = 8
Process completed.
2.编程实现Fibonacci数列。
• Fibonacci数列的定义为:
F1=1,
F2=1,
…
Fn=Fn-1+Fn-2 (n>=3)
• 提示:关键代码如下:
f[0]=f[1]=1;
for(i=2;i<10;i++)
f[i]=f[i-1]+f[i-2];
程序源代码如下:
class fibonacci{
int[] F;
fibonacci(int x){
F=new int[x];
F[0]=F[1]=1;
for(int i=2;i<F.length;i++){
F[i]=F[i-1]+F[i-2];
}
}
void shu(){
for(int i=0;i<F.length;i++){
System.out.print(F[i]+"\t");
}
}
}
public class Fibon {
public static void main(String[] args){
fibonacci f=new fibonacci(30);
f.shu();
}
public Fibon() {
}
}
3.编程采用冒泡法实现对数组元素由小到大排序。
• 冒泡法排序对相邻的两个元素进行比较,并把小的元素交换到前面。
• 提示:关键代码如下:
for(i=0;i<intArray.length-1;i++)
for(j=i+1;j<intArray.length;j++)
if(intArray[i]>intArray[j]){
t=intArray[i];intArray[i]=intArray[j];intArray[j]=t;
}
程序源代码如下:
public class paopao {
public static void main(String[] args){
int i,j,t;
int[] s={10,5,9,33,46,28,2,8,14,25};
for(i=0;i<s.length-1;i++)
for(j=i+1;j<s.length;j++)
if(s[i]>s[j]){
t=s[i];s[i]=s[j];s[j]=t;
}
for(i=0;i<s.length-1;i++){
System.out.print("s["+i+"]="+s[i]+"\t");
}
}
public paopao() {
}
}
建立使用数组的程序,本程序建立了一个长度为 5 的 1 维数组,一个长度为 12 的 2 维数组
编译结果为:a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
arr1(0,0) = 0
arr1(0,1) = 1
arr1(0,2) = 2
arr1(1,0) = 3
arr1(1,1) = 4
arr1(1,2) = 5
Process completed.
2).改进程序源代码:
public class KY5_1 {
public static void main(String args[]) {
int a[]=new int[5];
int arr1[][]=new int[3][4];
a[0]=10;
a[1]=10+a[0];
a[2]=30;
a[3]=40;
a[4]= a[1]+ a[2];
arr1[0][0]=0; arr1[0][1]=1; arr1[0][2]=2;
arr1[1][0]=3; arr1[1][1]=4; arr1[1][2]=5;
arr1[2][0]=6; arr1[2][1]=7; arr1[2][2]=8;
System.out.println("a["+0+"] = "+a[0]);
System.out.println("a["+1+"] = "+a[1]);
System.out.println("a["+2+"] = "+a[2]);
System.out.println("a["+3+"] = "+a[3]);
System.out.println("a["+4+"] = "+a[4]);
System.out.println("arr1("+0+","+0+") = "+arr1[0][0]);
System.out.println("arr1("+0+","+1+") = "+arr1[0][1]);
System.out.println("arr1("+0+","+2+") = "+arr1[0][2]);
System.out.println("arr1("+1+","+0+") = "+arr1[1][0]);
System.out.println("arr1("+1+","+1+") = "+arr1[1][1]);
System.out.println("arr1("+1+","+2+") = "+arr1[1][2]);
System.out.println("arr1("+2+","+0+") = "+arr1[2][0]);
System.out.println("arr1("+2+","+1+") = "+arr1[2][1]);
System.out.println("arr1("+2+","+2+") = "+arr1[2][2]);
}
}
3)改进后程序的编译结果:
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
arr1(0,0) = 0
arr1(0,1) = 1
arr1(0,2) = 2
arr1(1,0) = 3
arr1(1,1) = 4
arr1(1,2) = 5
arr1(2,0) = 6
arr1(2,1) = 7
arr1(2,2) = 8
Process completed.
2.编程实现Fibonacci数列。
• Fibonacci数列的定义为:
F1=1,
F2=1,
…
Fn=Fn-1+Fn-2 (n>=3)
• 提示:关键代码如下:
f[0]=f[1]=1;
for(i=2;i<10;i++)
f[i]=f[i-1]+f[i-2];
程序源代码如下:
class fibonacci{
int[] F;
fibonacci(int x){
F=new int[x];
F[0]=F[1]=1;
for(int i=2;i<F.length;i++){
F[i]=F[i-1]+F[i-2];
}
}
void shu(){
for(int i=0;i<F.length;i++){
System.out.print(F[i]+"\t");
}
}
}
public class Fibon {
public static void main(String[] args){
fibonacci f=new fibonacci(30);
f.shu();
}
public Fibon() {
}
}
3.编程采用冒泡法实现对数组元素由小到大排序。
• 冒泡法排序对相邻的两个元素进行比较,并把小的元素交换到前面。
• 提示:关键代码如下:
for(i=0;i<intArray.length-1;i++)
for(j=i+1;j<intArray.length;j++)
if(intArray[i]>intArray[j]){
t=intArray[i];intArray[i]=intArray[j];intArray[j]=t;
}
程序源代码如下:
public class paopao {
public static void main(String[] args){
int i,j,t;
int[] s={10,5,9,33,46,28,2,8,14,25};
for(i=0;i<s.length-1;i++)
for(j=i+1;j<s.length;j++)
if(s[i]>s[j]){
t=s[i];s[i]=s[j];s[j]=t;
}
for(i=0;i<s.length-1;i++){
System.out.print("s["+i+"]="+s[i]+"\t");
}
}
public paopao() {
}
}