实验目的:
(1)掌握一维数组和二维数组的定义、初始化方法。
(2)了解和初步应用java.lang.Math类的random()方法处理实际问题。
(3)了解增强for循环,并使用增强for循环顺序访问数组元素。
(4)掌握String类中split方法、charAt方法以及length方法的使用。
(5)掌握Double、Integer等数据包装类的parseDouble、parseInt等方法。
(6)掌握数组的length属性的应用
实验内容:
(1)编写一个程序,使用命令行参数的方式从控制台读入一组整数,利用foreach循环对其进行求和并输出结果。
package project3;
import java.util.*;
public class Foreach {
public static void main(String[] args) {
int[] a=new int[5];
int sum=0;
@SuppressWarnings("resource")
Scanner input=new Scanner(System.in);
for(int i=0;i<5;i++) {
a[i]=input.nextInt();
}
for(int i:a) {
sum+=i;
}
System.out.println("它们的和为"+sum);
}
}
(2)分别用一维数组(例子数组如下 { 7, 4, 3, 9, 0, 6 })实现冒泡排序、选择排序和插入排序中的两种排序算法,程序中要求加注释。
package project3;
public class Sort {
public static void main(String[] args) {
int[] a={ 7, 4, 3, 9, 0, 6 };
sort1(a,6);
sort2(a,6);
}
//冒泡排序(从小到大排序)
public static void sort1(int[] a,int n) {
for(int i=0;i<n-1;i++) { //有n个元素,一共需要进行n-1趟比较
for(int j=0;j<n-1-i;j++) { //每趟需要进行n-1-j次比较
if(a[j]>a[j+1]) { //如果后边的元素小于前面的元素,前后元素的值互换,否则不换
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
System.out.println("冒泡法排序:");
for(int i=0;i<n;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
//选择排序
public static void sort2(int[] a,int n) {
int k=0;
for(int i=0;i<n-1;i++) {//进行n-1次操作
k=i;
for(int j=i+1;j<n;j++) { //从a[i+1]到a[n-1]中选出最小的元素,与a[i]交换
if(a[k]>a[j]) {
k=j;
}
int t=a[k];
a[k]=a[i];
a[i]=t;
}
}
System.out.println("选择法排序:");
for(int i=0;i<n;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
}
(3)编写程序实现两个矩阵的相加、相乘。
要求程序运行结果形如如下显示:
Array c
1 2 3
4 5 6
7 8 9
Array d
2 2 2
1 1 1
3 3 3
Array c+d
3 4 5
5 6 7
10 11 12
Array c*d
13 13 13
31 31 31
49 49 49
package project3;
public class Array {
static int e=0;
public static void main(String[] args) {
int[][] c= {{1,2,3},{4,5,6},{7,8,9}};
int[][] d= {{2,2,2},{1,1,1},{3,3,3}};
System.out.println("Array c");
outArray(c);
System.out.println("Array d");
outArray(d);
System.out.println("Arrray c+d");
Arrayadd(c,d);
System.out.println("Arrray c*d");
ArrayX(c,d);
}
public static void outArray(int[][] a) {
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void Arrayadd(int[][] a,int[][] b ) {
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
System.out.print(a[i][j]+b[i][j]+" ");
}
System.out.println();
}
}
public static void ArrayX(int[][] a,int[][] b ) {
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
e=0;
for(int k=0;k<a.length;k++) {
e+=a[i][k]*b[k][j];
}
System.out.print(e+" ");
}
System.out.println();
}
}
}
(4)将用“;”和“,”分割的包含数字字符的字符串“23, 21.3, 33;34, 2, 1.9, 2.1;3, 3, 1, 3, 4, 4.9”中的数据解析出来放在一个double类型的二维数组中,以分号分割二维数组的每一行,以逗号分割每行中的各个元素。(利用String 的split方法)
package project3;
public class Split {
public static void main (String[] args){
String s= "23, 21.3, 33;34, 2, 1.9, 2.1;3, 3, 1, 3, 4, 4.9";
separate(s);
}
public static void separate(String s) {
String[] s1 = s.split(";");
double[][] a = new double[s1.length][];
for(int i = 0;i < s1.length;i++)
{
String[] s2 = s1[i].split(",");
a[i] = new double[s2.length];
for(int j = 0;j < s2.length;j++)
a[i][j] = Double.parseDouble(s2[j]);
}
for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
5)查看帮助、编写例子
利用System类中的arraycopy()方法复制数组。
分别用Arrays类中的sort方法和binarySearch方法实现数组的排序和折半查找。
package project3;
import java.util.*;
public class Copy_seek {
public static void main(String[] args) {
int []a = {0,1,9,819,-616,78,516,-54,6};
show(a);
showagain(a);
}
public static void show(int[] a) {
for(int i = 0;i < a.length;i++)
System.out.print(a[i]+" ");
}
//复制并排序
//复制
public static void showagain(int[] a) {
int[] a1 = new int[a.length];
System.arraycopy(a, 0, a1, 0, a.length);
System.out.println("\n复制之后的数组:");
for(int i = 0;i < a1.length;i++)
System.out.print(a1[i]+" ");
//排序
Arrays.sort(a1);
System.out.println("\n排序之后的数组:");
for(int i = 0;i < a1.length;i++) {
System.out.print(a1[i]+" ");
}
System.out.println();
//折半查找
System.out.println("折半查找:");
for(int i = 0;i < a1.length;i++) {
System.out.println(a1[i]+"的下标为:"+Arrays.binarySearch(a1, a1[i]));
}
}
}
(6)随机生成100个小写字母,统计每个字母出现的次数,并显示出来。
(利用Math.random()方法随机产生)
package project3;
import java.util.*;
@SuppressWarnings("unused")
public class Letter {
public static void main(String[] args) {
char []c = new char[100];
for(int i = 0;i < 100;i++){
c[i] = (char)( 'a' + Math.random()*('z'-'a'+1));
}
show(c);
sortletter(c);
}
public static void sortletter(char[] c) {
int []num = new int[26];
char a = 'a';
for(int i =0;i < 99;i++)
num[c[i]-a] = num[c[i]-a] + 1;
System.out.println("统计结果:");
for(int i =0;i < 99;i++){
if(i%5==0&&i!=0)
System.out.println();
System.out.print((char)(a+i) + " : " + num[i] + " ");
}
System.out.println();
}
public static void show(char[] c) {
System.out.println("产生的字母为:");
for(int i = 0;i < 100;i++){
if(i%5==0&&i!=0)
System.out.println();
System.out.print(c[i]+" ");
}
System.out.println();
}
}
(7)建立一个不规则的二维数组如下,并在控制台显示,数组如下
1 3 5
2 4 6 8
1 9 16 25 36
10 20 30
1 2 3 4 5 6
package project3;
public class Odd {
public static void main(String[] args) {
int[][] a= {{1,3,5},{2,4,6,8},{1,9,16,25,36},{10,20,30},{1,2,3,4,5,6}};
show(a);
}
public static void show(int[][] a) {
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
(8)编写两个重载的方法分别交换两个整型变量,和整型数组的第一个和第二个元素,运行并分析结果
package project3;
public class Swap {
public int x=1,y=2;
public static void main(String[] args) {
Swap s=new Swap();
int[]a= {0,1,2,3,4,5};
System.out.println("交换前:x="+s.x+",y="+s.y);
System.out.println("数组a");
show(a);
myswap(s);
System.out.println("交换后:x="+s.x+",y="+s.y);
myswap(a);
System.out.println("数组a");
show(a);
}
public static void myswap(Swap s) {
int t=s.x;
s.x=s.y;
s.y=t;
}
public static void myswap(int[] a) {
int t=a[0];
a[0]=a[1];
a[1]=t;
}
public static void show(int[] a) {
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
}