当前位置:网站首页>数组练习 后续补充
数组练习 后续补充
2022-06-27 17:53:00 【往日如风_】

1、数组平均值
public static double avg(int[] array){
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum*(1.0) / array.length;
}
public static void main(String[] args) {
int[] array = {5,65,21,34,79,63};
System.out.println(avg(array));
}
运行代码:
44.5
Process finished with exit code 0
2、查找数组中指定元素(顺序查找)
public static int key(int[] array,int key){
for (int i = 0; i < array.length; i++) {
if (array[i]==key){
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6};
int index = key(array,5);
if (index == -1){
System.out.println("数组中没有你要找的关键字!");
}else {
System.out.println("找到了你要的关键字,下标为:"+index);
}
}
运行代码:
找到了你要的关键字,下标为:4
Process finished with exit code 0
3、二分查找
public static int two(int[] array,int key) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (array[mid] > key) {
right = mid - 1;
} else if (array[mid] < key) {
left = mid + 1;
} else {
return mid;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {1,2,3,14,5};
Arrays.sort(array);
System.out.println("排序之后的数组:"+Arrays.toString(array));
int index = two(array,5);
if (index == -1){
System.out.println("数组中没有你要找的关键字!");
}else {
System.out.println("找到了你要的关键字,下标为:"+index);
}
}
运行代码:
排序之后的数组:[1, 2, 3, 5, 14]
找到了你要的关键字,下标为:3
Process finished with exit code 0
4、冒泡排序
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 tmp = array[j];
array[j]=array[j+1];
array[j+1] = tmp;
}
}
}
}
public static void main(String[] args) {
int[] array = {1,2,31,14,5};
System.out.println("排序之前,数组为:"+ Arrays.toString(array));
bubbleSort(array);
System.out.println("排序之后,数组为:"+ Arrays.toString(array));
}
运行代码:
排序之前,数组为:[1, 2, 31, 14, 5]
排序之后,数组为:[1, 2, 5, 14, 31]
Process finished with exit code 0
优化一下:不变化就结束
public static void bubbleSort(int[] array){
for (int i = 0; i < array.length-1; i++) {
boolean flg = false;
for (int j = 0; j < array.length-1-i; j++) {
if (array[j]>array[j+1]){
int tmp = array[j];
array[j]=array[j+1];
array[j+1] = tmp;
flg = true;
}
}
if (flg == false) break;;
}
}
public static void main(String[] args) {
int[] array = {1,2,31,14,5};
System.out.println("排序之前,数组为:"+ Arrays.toString(array));
bubbleSort(array);
System.out.println("排序之后,数组为:"+ Arrays.toString(array));
}
排序之前,数组为:[1, 2, 31, 14, 5]
排序之后,数组为:[1, 2, 5, 14, 31]
Process finished with exit code 0
5、数据逆置
public static void main(String[] args) {
int[] array = {1,2,31,14,5};
System.out.println("逆置之前,数组为:"+ Arrays.toString(array));
reverse(array);
System.out.println("逆置之后,数组为:"+ Arrays.toString(array));
}
public static void reverse(int[] array){
int left = 0;
int right = array.length-1;
int tmp = 0;
while (left<right){
tmp = array[left];
array[left] = array[right];
array[right]= tmp;
left++;
right--;
}
}
逆置之前,数组为:[1, 2, 31, 14, 5]
逆置之后,数组为:[5, 14, 31, 2, 1]
Process finished with exit code 0
6、二维数组
优化一下:不变化就结束定义
public static void main(String[] args) {
//手动加花括号
int[][] array = {
{1,2,3},{4,5,6}};
//也不能填数字
int[][] array2 = new int[][]{
{1,2,3},{4,5,6}};
//不晓得数值情况 默认0
int[][] array3 = new int[2][3];
}
优化一下:不变化就结束打印: 很麻烦
public static void main(String[] args) {
int[][] array = {
{1,2,3},{4,5,6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
运行代码:
1 2 3
4 5 6
Process finished with exit code 0
优化一下:不变化就结束万能打印:
public static void main(String[] args) {
int[][] array = {
{1,2,3},{4,5,6}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
运行代码:
1 2 3
4 5 6
Process finished with exit code 0
优化一下:不变化就结束二维数组示意图:
优化一下:不变化就结束使用foreach进行打印:
System.out.println("使用foreach进行打印");
for (int[] tmp:array) {
System.out.println(tmp);
for (int x: tmp) {
System.out.print(x+" ");
}
System.out.println();
}
运行代码:
使用foreach进行打印
[[email protected]
1 2 3
[[email protected]
4 5 6
Process finished with exit code 0
优化一下:不变化就结束使用toString方法进行打印:
System.out.println("使用toString方法进行打印");
//深度打印方法Arrays.deepToString()
System.out.println(Arrays.deepToString(array));
运行代码:
使用toString方法进行打印
[[1, 2, 3], [4, 5, 6]]
Process finished with exit code 0
边栏推荐
- On thread safety
- Comment encapsuler un appel à une bibliothèque
- maxwell 报错(连接为mysql 8.x)解决方法
- The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder
- 【建议收藏】ABAP随笔-EXCEL-4-批量导入-推荐
- Function key input experiment based on stm32f103zet6 Library
- Running lantern experiment based on stm32f103zet6 library function
- Market status and development prospect forecast of the global shuttleless air jet loom industry in 2022
- 信息学奥赛一本通 1333:【例2-2】Blah数集 | OpenJudge NOI 3.4 2729:Blah数集
- 海底电缆探测技术总结
猜你喜欢

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

Oracle 获取月初、月末时间,获取上一月月初、月末时间

DCC888 :Register Allocation

拥抱云原生:江苏移动订单中心实践

基于STM32F103ZET6库函数蜂鸣器实验

C# 二维码生成、识别,去除白边、任意颜色

SQL Server - Window Function - 解决连续N条记录过滤问题

金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东

Bit. Store: long bear market, stable stacking products may become the main theme

【登录界面】
随机推荐
Gartner聚焦中国低代码发展 UniPro如何践行“差异化”
Online text batch inversion by line tool
海底电缆探测技术总结
PCB线路板蛇形布线要注意哪些问题?
Seven phases of CMS implementation
Blink SQL内置函数大全
International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding
New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million
ABAP随笔-EXCEL-3-批量导入(突破标准函数的9999行)
工作流自动化 低代码是关键
Code and principle of RANSAC
基于STM32F103ZET6库函数蜂鸣器实验
【云驻共创】 什么是信息化?什么是数字化?这两者有什么联系和区别?
OpenSSL client programming: SSL session failure caused by an obscure function
【登录界面】
Market status and development prospect forecast of global aircraft hose industry in 2022
華大單片機KEIL報錯_WEAK的解决方案
可靠的分布式锁 RedLock 与 redisson 的实现
Keras deep learning practice (12) -- facial feature point detection
Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022