实验 3 方法和数组

实验目的:

(1)掌握一维数组和二维数组的定义、初始化方法。

(2)了解和初步应用java.lang.Math类的random()方法处理实际问题。

(3)了解增强for循环,并使用增强for循环顺序访问数组元素。

(4)掌握String类中split方法、charAt方法以及length方法的使用。

(5)掌握Double、Integer等数据包装类的parseDouble、parseInt等方法。

(6)掌握数组的length属性的应用

实验内容:(要求把源程序和运行结果图都粘贴到实验报告中)

(1)编写一个程序,使用命令行参数的方式从控制台读入一组整数,利用foreach循环对其进行求和并输出结果。

程序源代码

import java.util.Scanner;
public class lab3_1 {
public static void main(String args[]) {
Integer[] a = new Integer[5];
Scanner sca = new Scanner(System.in);
for(int i=0;i<5;i++){
a[i]=sca.nextInt();
}
int sum=0;
for(int c:a){
System.out.println("你输入的数字为:"+c);
sum+=c;
}
System.out.println(sum);
}
}


 

程序运行结果贴图

(2)分别用一维数组(例子数组如下 { 7, 4, 3, 9, 0, 6 })实现冒泡排序、选择排序和插入排序中的两种排序算法,程序中要求加注释。

程序代码



public class Sort {
public static void bubbleSort(int[] array) {
// 外层循环控制轮数
for (int i = 0; i < array.length - 1; i++) {
// 内层循环控制每轮比较的次数
for (int j = 0; j < array.length - 1 - i; j++) {
// 如果前一个数大于后一个数,就交换位置
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
// 选择排序
public static void selectionSort(int[] array) {
// 外层循环控制每次选择的数字
for (int i = 0; i < array.length - 1; i++) {
// 内层循环控制找到最小数的位置
int minIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// 将最小数和当前位置交换
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
// 插入排序
public static void insertionSort(int[] array) {
// 从第二个数开始,将每个数插入到已经排好序的数列中
for (int i = 1; i < array.length; i++) {
int temp = array[i];
int j = i;
// 将当前数和已经排好序的数列从后往前比较,如果当前数小,就将已经排好序的数向后移一位
while (j > 0 && temp < array[j - 1]) {
array[j] = array[j - 1];
j--;
}
// 将当前数插入到合适的位置
array[j] = temp;
}
}
public static void main(String[] args) {
int[] a = {7, 4, 3, 9, 0, 6};
int[] b = {7, 4, 3, 9, 0, 6};
int[] c = {7, 4, 3, 9, 0, 6};
bubbleSort(a);
selectionSort(b);
insertionSort(c);
System.out.println("\n-------bubbleSort-------");
for(int i =0; i<a.length; i++){
System.out.print(a[i]+" ");
}
System.out.println("\n-----selectionSort------");
for(int i =0; i<a.length; i++){
System.out.print(b[i]+" ");
}
System.out.println("\n-----insertionSort------");
for(int i =0; i<a.length; i++){
System.out.print(c[i]+" ");
}
}
}

 

运行结果贴图:

 

(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

程序代码

public class Matrix {
private int rows;
private int cols;
private int[][] data;

public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.data = new int[rows][cols];
}

public int getRows() {
return rows;
}

public int getCols() {
return cols;
}

public int get(int i, int j) {
return data[i][j];
}

public void set(int i, int j, int value) {
data[i][j] = value;
}

public Matrix add(Matrix other) {
if (this.rows != other.rows || this.cols != other.cols) {
throw new IllegalArgumentException("Matrices must have the same dimensions to be added");
}
Matrix result = new Matrix(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.set(i, j, this.get(i, j) + other.get(i, j));
}
}
return result;
}

public Matrix multiply(Matrix other) {
if (this.cols != other.rows) {
throw new IllegalArgumentException("Number of columns in the first matrix must match number of rows in the second matrix");
}
Matrix result = new Matrix(this.rows, other.cols);
for (int i = 0; i < result.rows; i++) {
for (int j = 0; j < result.cols; j++) {
int sum = 0;
for (int k = 0; k < this.cols; k++) {
sum += this.get(i, k) * other.get(k, j);
}
result.set(i, j, sum);
}
}
return result;
}
public void show(){
for(int i =0;i<this.rows;i++){
for(int j=0; j<this.cols; j++){
System.out.print(this.data[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] a){
Matrix matrix1 = new Matrix(3, 3);
matrix1.set(0, 0, 1);
matrix1.set(0, 1, 2);
matrix1.set(0, 2, 3);
matrix1.set(1, 0, 4);
matrix1.set(1, 1, 5);
matrix1.set(1, 2, 6);
matrix1.set(2, 0, 7);
matrix1.set(2, 1, 8);
matrix1.set(2, 2, 9);
System.out.println("Array c");
matrix1.show();
Matrix matrix2 = new Matrix(3, 3);
matrix2.set(0, 0, 2);
matrix2.set(0, 1, 2);
matrix2.set(0, 2, 2);
matrix2.set(1, 0, 1);
matrix2.set(1, 1, 1);
matrix2.set(1, 2, 1);
matrix2.set(2, 0, 3);
matrix2.set(2, 1, 3);
matrix2.set(2, 2, 3);
System.out.println("Array d");
matrix2.show();

Matrix sum = matrix1.add(matrix2);
System.out.println("Array c+d");
sum.show();

Matrix mul = matrix1.multiply(matrix2);
System.out.println("Array c*d");
}
}





运行结果贴图:

 

(4)将用“;”和“,”分割的包含数字字符的字符串“23, 21.3, 33;34, 2, 1.9, 2.1;3, 3, 1, 3, 4, 4.9”中的数据解析出来放在一个double类型的二维数组中,以分号分割二维数组的每一行,以逗号分割每行中的各个元素。(利用String 的split方法)

程序代码



 

public class lab3_4 {
public static void main (String args[]){

String[]str = ("3, 21.3, 33; 34, 2, 1.9, 2.1; "
+ "3, 3, 1, 3, 4, 4.9").split(";");//将每一行分开
double [][]num = new double[str.length][]; //建立最终的储存数组

for(int i = 0;i < str.length;i++)
{
String []temp = str[i].split(","); //在某行中数字按","分开
num[i] = new double[temp.length];
for(int j = 0;j < temp.length;j++)
num[i][j] = Double.parseDouble(temp[j]); //转换为double数组
}

for(int i = 0;i < num.length;i++)
{
for(int j = 0;j < num[i].length;j++)
{
System.out.print(num[i][j]+" ");
}
System.out.println();
}


}
}

运行结果贴图:

(5)查看帮助、编写例子

利用System类中的arraycopy()方法复制数组。

分别用Arrays类中的sort方法和binarySearch方法实现数组的排序和折半查找。

程序代码



 

import java.util.Arrays;

public class lab3_5 {
public static void main (String args[]){

int []a = {1,6,8,4,2,13,5,7,9,10};
//输出原数组
for(int i = 0;i < a.length;i++)
System.out.print(a[i]+" ");

//复制数组并输出
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]+" ");
}
}


 

运行结果贴图:

 

(6)随机生成100个小写字母,统计每个字母出现的次数,并显示出来。

(利用Math.random()方法随机产生)

程序代码



 

public class lab3_6 {
public static void main(String []args){
char []s = new char[100];
char a = 'a';

//随机产生小写字母
for(int i = 0;i < 100;i++){
s[i] = (char)( 'a' + Math.floor(Math.random()*('z'-'a'+1)) );
}

int []num = new int[26];
//统计字母个数
for(int i =0;i < 99;i++)
num[s[i]-a] = num[s[i]-a] + 1;

for(int i =0;i < 99;i++){
System.out.print((char)(a+i) + " : " + num[i] + " ");
if(i%5==0)
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

程序代码

public class lab3_7{

public static void main(String[] args) {
int[][] a = new int[5][];
a[0] = new int[3];
a[1] = new int[4];
a[2] = new int[5];
a[3] = new int[3];
a[4] = new int[6];

//赋值
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = (int)(Math.random()*100+1);
System.out.print(a[i][j]+" ");
}
System.out.println();
}

}

}


 

运行结果贴图:

 

(8)编写两个重载的方法分别交换两个整型变量,和整型数组的第一个和第二个元素,运行并分析结果

程序代码
 

public class lab3_8 {

public static void main(String []args){
Num a = new Num(110);
Num b = new Num(120);
int []c = {3,4};
System.out.println("交换前:");
System.out.println("a=" + a.number + " " + "b=" + b.number);
System.out.println("c[0]=" + c[0] + " " + "c[1]=" + c[1]);
swap(a,b);
swap(c);
System.out.println("交换后:");
System.out.println("a=" + a.number + " " + "b=" + b.number);
System.out.println("c[0]=" + c[0] + " " + "c[1]=" + c[1]);
}

public static void swap(Num a,Num b){
int temp = a.number;
a.number = b.number;
b.number = temp;
}

public static void swap(int []c){
int t;
t = c[0];
c[0] = c[1];
c[1] = t;
}
}

class Num {
int number;
public Num(int number){
this.number=number;
}
}






运行结果贴图:

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

+720

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值