当前位置:文档之家› 实验 3 方法和数组

实验 3 方法和数组

}
double[] list2 = {9,3,6,1,5,2,0,8};
System.out.print("\n进行冒泡排序函数调用进行排序:");
Sort.bubbleSort(list2);//对冒泡排序函数调用
for(inti = 0; i < list2.length; i++){
System.out.print(list2[i] +" ");
list[i] = list[i + 1];
list[i + 1] = m;
}
}
}
}
publicstaticvoidinsertionSort(double[] list) {
//插入排序函数,对数组进行从小到大的排序
for(inti = 0; i < list.length; i++) {
doublecurrentElement = list[i];
(4)掌握String类中split方法、charAt方法以及length方法的使用。
(5)掌握Double、Integer等数据包装类的parseDouble、parseInt等方法。
(6)掌握数组的length属性的应用
实验内容:(要求把源程序和运行结果图都粘贴到实验报告中)
(1)分别用一维数组(例子数组如下{ 7, 4, 3, 9, 0, 6 })实现冒泡排序、选择排序和插入排序中的两种排序算法,程序中要求加注释。
程序代码:
packagewenhui;
publicclassSort {
//排序方法
publicstaticvoidselectionSort(double[] list) {
//选择排序函数,对数组进行从小到大的排序
for(inti = 0; i < list.length- 1; i++) {
//在i——list.length - 1中找到最小值并放在i位置上
//主函数
double[] list = { 7, 4, 3, 9, 0, 6};
System.out.print("进行插入排序函数调用进行排序:");
Sort.insertionSort(list);//对插入排序函数调用
for(inti = 0; i < list.length; i++){
System.out.print(list[i] +" ");
for(inti = 0;i < array1.length;i++){
for(intj = 0;j < array2[0].length;j++){
array[i][j] = array1[i][j] + array2[i][j];
}
}
returnarray;
}
publicstaticint[][]matrixMultiplication(int[][] array1,int[][] array2){
//矩阵乘法运算
int[][] array =newint[array1.length][array2[0].length];
}
double[] list1 = {2,8,3,9,1,5,0};
System.out.print("\n进行选择排序函数调用进行排序:");
Sort.selectionSort(list1);//对选择排序函数调用
for(inti = 0; i < list1.length; i++){
System.out.print(list1[i] +" ");
}
}
}
运行结果贴图:
(2)编写程序实现两个矩阵的相加、相乘。
要求程序运行结果形如如下显示:
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
12 12 12
21 21 21
30 30 30
程序代码:
intk;
for(k = i - 1; k >= 0 && list[k] > currentElement; k--) {
list[k +1] = list[k];
}
list[k +1] = currentElement;
}
}
}
packagewenhui;
publicclassTest {
publicstaticvoidmain(String[] args) {
packagewenhui;
publicclassMatrix {
publicstaticint[][] matrixAdd(int[][] array1,int[][] array2){
//矩阵加法运算
int[][] array =newint[array1.length][array1[0].length];
list[m] = list[i];
list[i] = min;
}
}
}
publicstaticvoidbubbleSort(double[] list) {
//冒泡排序函数,对数组进行从小到大的排序
doublem = 0.0;
for(intj = 0;j < list.length-1;j++){
//每次将一个大的往后移动到位
for(inti = 0;i < list.length- 1 - j; i++){
//从i——list.length - 1 - j中找到最大的值并放在下标为list.length - 1 - j的位置上
if(list[i] > list[i + 1]){
//俩个数相比较大的往后移,小的往前移
m = list[i];
山西大学计算机与级
软件工程1班
课程名称
Java实验
实验日期
2013/11/6
成绩
指导教师
批改日期
实验名称
实验3方法和数组
实验目的:
(1)掌握一维数组和二维数组的定义、初始化方法。
(2)了解和初步应用ng.Math类的random()方法处理实际问题。
(3)了解增强for循环,并使用增强for循环顺序访问数组元素。
doublemin = list[i];
intm = i;
for(intj = i + 1; j < list.length; j++) {
if(min >= list[j]) {
//记录最小数的值和其下标值
min = list[j];
m = j;
}
}
if(m != i) {
//如果有小的数就进行交换
相关主题