arr

数据结构:冒泡排序(bubble sort)

社会主义新天地 提交于 2020-01-10 08:30:45
public class BubbleSort { public static void main(String[] args) { int[] arr = {3,9,-1,10,20}; bubbleSort(arr); for (int i = 0; i < arr.length; i++) { System.out.printf(arr[i]+" "); } } static void bubbleSort(int[] arr){ for (int i = 0; i < arr.length-1; i++) { boolean isChange = false; for (int j = 0; j < arr.length-i-1; j++) { if(arr[j] > arr[j+1]){ int tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; isChange = true; } } if(!isChange){ break; } } } } 来源: CSDN 作者: 请叫我大虾 链接: https://blog.csdn.net/m0_37564426/article/details/103846422

js filter() es6 方法总结

≯℡__Kan透↙ 提交于 2020-01-10 07:41:39
本文转自: https://blog.csdn.net/bossxu_/article/details/80756563 1.创建一个数组,判断数组中是否存在某个值 var newarr = [ { num : 1, val : 'ceshi' , flag : 'aa' } , { num : 2, val : 'ceshi2' , flag : 'aa2' } ] console. log ( newarr. filter ( item => item.num===2 ) ) 2.去掉空数组空字符串、undefined、null var arr = [ '1' , '2' ,undefined, '3.jpg' ,undefined] var newArr = arr. filter ( item => item ) console. log ( newArr ) var arr = [ '1' , '2' ,null, '3.jpg' ,null] var newArr = arr. filter ( item => item ) console. log ( newArr ) >//空字符串里面不能包含空格 var arr = [ '1' , '2' , '' , '3.jpg' , '' ] var newArr = arr. filter ( item => item

数据处理的一些方法

被刻印的时光 ゝ 提交于 2020-01-10 03:57:03
保留小数点后两位:alert(num.toFixed(2)); yuanToCent(yuan){ var arr = String(yuan).split("."); var a = arr[0]+(arr.length>1?(arr[1].length==1?(arr[1]+"0"):arr[1].substring(0,2)):"00"); return parseInt(a) ; }, yuanToCent(5.55)/100 //调用时需要除以100之后数值才是对应的 下面是针对特殊数字精度的处理 <html> <head> <script> function countMoney(){ var totalMoney = document.querySelector("#totalMoney").value; var noMoney = document.querySelector("#noMoney").value; var rate = document.querySelector("#rate").value; var result = _countMoneyToCents(totalMoney,noMoney,rate); document.querySelector("#result").value = centToYuan(result); document

想减少程序运行的BUG吗?那么来了解一下纯函数吧--减少BUG利器

有些话、适合烂在心里 提交于 2020-01-09 20:15:12
什么是纯函数? 纯函数是在调用该函数,函数执行的时候,不会修改全局变量,不依赖与全局变量,不改变其他的作用域,并且一定要返回值。总体上来说满足3个条件。 (1.有返回值(return) 2.不依赖于全局变量 3.不改变其他的作用域(GO),是利用的自己的AO(在传递参数的时候实现)) 反例: var num = 18; function compare(x){ return x > num; } console.log(compare(20)); //true 解释:这个函数不是纯函数,因为返回值依赖外部作用域的num。 纯函数 function compare(x, num){ return x > num; } console.log(compare(20, 18)); //true 解释:这个函数只与传进来的参数有关,不依赖外界,与其无关的作用域里的变量无关;同时,函数执行完毕后,没有对外界产生影响(没有修改外界的变量的值)。 接着看 var num = 18; function compare(x, num){ //预编译时,使用的是compare函数作用域的num,依旧是纯函数 //num = 19; //代码片段1(此处不用看,在下一步与eg4对比中使用) return x > num; } console.log(compare(20, num)); //true /

创建自定义类型方式

前提是你 提交于 2020-01-09 19:22:12
1、 构造函数和原型函数 function Money ( value , name , count ) { this . value = value ; this . name = name ; this . count = count ; } Money . prototype = { constructor : Money , getMoney : function ( ) { return this . value * this . count ; } } mon . getMoney ( ) === > 100 2、动态原型模式 (此方法不能重写构造函数原型,否则对象获取不到新增属性或方法) function Money1 ( value , name , count ) { this . value = value ; this . name = name ; this . count = count ; if ( typeof this . some != "function" ) { Money1 . prototype . some = 222 ; console . log ( "打印出:" + 123456 ) } } let mo2 = new Money1 ( 10 , 'kk' , 1 ) ; === = 打印出: 123456 3、寄生构造函数

try,catch,finally尝试(一个程序块多个catch)

点点圈 提交于 2020-01-09 16:26:12
曾学过c++,但是对这些异常捕捉不是很了解,通过别的编程语言了解 public class newclass { public static void main(String[] args) { try {int [] arr=new int[5]; arr[1]=0; // arr[10]=1; arr[0]=3; int res=arr[0]/arr[1]; } catch(ArrayIndexOutOfBoundsException ex2) { System.out.println("2"); ex2.printStackTrace(); } catch(ArithmeticException ex1){ System.out.println("1"); ex1.printStackTrace(); } catch(Exception ex){ex.printStackTrace(); } finally{ System.out.println("不存在的");} System.out.println("为什么急着");} } 总结 一个程序块多个catch只会处理符合的那个,其他自定义的catch异常不会处理,而数组越界是编译器最早发现的,所以最早抛出错误,所以是2. 来源: https://www.cnblogs.com/otakus/p/12171808.html

日常代码随笔

时光总嘲笑我的痴心妄想 提交于 2020-01-08 23:39:49
1,this指向问题的代码: var _getElementById = document.getElementById; document.getElementById = function(id){ console.log(1); return _getElementById(id); } var button = document.getElementById( 'button' ); //Uncaught TypeError: Illegal invocation at HTMLDocument.document.getElementById (<anonymous>:5:12) at <anonymous>:8:23 异常发生在_getElementById(id)这句,此为一个全局函数,调用全局函数时候this指向是window,而document.getElementById内部实现this指向是document。所以需要在调用时候将this指向document对象。改动后代码如下: document.getElementById = function(){ console.log(1); return _getElementById.apply(document,arguments); //此处document可用this代替 } var button =

java数据结构之常用排序算法

半城伤御伤魂 提交于 2020-01-08 15:48:35
冒泡排序 private void maopao(int arr[]) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } 选择排序 private void xuanze(int[] arr) { for (int i = 0; i < arr.length; i++) { int min = arr[i]; int x = -1; boolean b = false; for (int j = i; j < arr.length; j++) { if (arr[j] < min) { b = true; min = arr[j]; x = j; } } if (b) { int temp = arr[i]; arr[i] = min; arr[x] = temp; } } } 插入排序 1 private void charu(int[] arr2) { 2 int[] arr = new int[arr2.length]; 3 for (int i = 0; i < arr2

数组扁平化

ε祈祈猫儿з 提交于 2020-01-08 14:07:34
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 扁平化 var arr = [1, [2, [3, 4]]]; console.log(flatten(arr)) // [1, 2, 3, 4] 实现 递归 var arr = [1, [2, [3, 4]]]; function flatten(arr) { var result = []; for (var i = 0, len = arr.length; i < len; i++) { if (Array.isArray(arr[i])) { result = result.concat(flatten(arr[i])) } else { result.push(arr[i]) } } return result; } console.log(flatten(arr)) toString var arr = [1, [2, [3, 4]]]; function flatten(arr) { return arr.toString().split(',').map(function(item){ return +item }) } console.log(flatten(arr)) reduce var arr = [1, [2, [3, 4]]]; function flatten(arr) {

求数组中的最大值最小值

拜拜、爱过 提交于 2020-01-08 13:46:43
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Math.max Math.max(true, 0) // 1 Math.max(true, '2', null) // 2 Math.max(1, undefined) // NaN Math.max(1, {}) // NaN 原始方法 var arr = [6, 4, 1, 8, 2, 11, 23]; var result = arr[0]; for (var i = 1; i < arr.length; i++) { result = Math.max(result, arr[i]); } console.log(result); reduce var arr = [6, 4, 1, 8, 2, 11, 23]; function max(prev, next) { return Math.max(prev, next); } console.log(arr.reduce(max)); 排序 var arr = [6, 4, 1, 8, 2, 11, 23]; arr.sort(function(a,b){return a - b;}); console.log(arr[arr.length - 1]) eval var arr = [6, 4, 1, 8, 2, 11, 23]; var max =