说明:最近接触到一些js的功能代码,然后发现很多基础都不会弄,
尤其是涉及到数据处理的,java里面常用的数组 ,list ,map,转到js里面,就完全搞不明白,一头雾水,非常影响工作效率,
因此弄了这个小demo,用来记录一下js里面的常用处理数据的一些工具类写法。
js篇
step1: map的基本用法
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-user',
standalone: true,
imports: [],
templateUrl: './user.component.html',
styleUrl: './user.component.css'
})
export class UserComponent implements OnInit{
constructor() {
console.log('constructor')
}
ngOnInit(): void {
console.log('ngOnInit')
name.set('a', 1);
name.set('b', 2);
name.set('c', 3);
console.log(name);
console.log(name.get('a'));
name.set('a', 97);
console.log(name.get('a'));
console.log(name);
console.log(name.size);
name.delete('b');
console.log(name);
console.log(name.size);
}
}
var name = new Map();
step2: js数组的基本用法,相当于java的arraylist,只不过js里面的数组非常的随意,或者叫自由,数组里基本啥都能装,什么乱起八糟的都能往里面塞,也没有固定长度,对于那些写惯了java的人看来,这很离谱
import {Component, OnInit} from '@angular/core';
@Component({
selector: 'app-user',
standalone: true,
imports: [],
templateUrl: './user.component.html',
styleUrl: './user.component.css'
})
export class UserComponent implements OnInit{
constructor() {
console.log('constructor')
}
ngOnInit(): void {
console.log('ngOnInit')
console.log(mycars)
console.log(mycars2)
var name=mycars2[2]; //获取 修改
mycars2[0]="Opel";
console.log(name)
console.log(mycars2)
console.log(apples)
console.log(apples2)
console.log(apples3)
var bannale = mycars2[3]
console.log(bannale)
mycars2[4] = "无解"
console.log(mycars2)
myArray = myArray.filter(item => item !== "banana");
console.log(myArray); // ["apple", "orange", "grape"]
mycars2 = myArray
console.log(myArray)
myArray.push('测试数据')
console.log(myArray)
}
}
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
var mycars2=new Array("Saab","Volvo","BMW","奥迪");
var apples= new Array()
var size=10
var apples2 = new Array(size)
var apples3 = new Array(1,2,3,4)
let myArray = ["apple", "banana", "orange", "grape", "bananas", "grape", "bananas"];
=我是分割线====
java篇章
step1: java数组
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Num {
public static void main(String[] args) {
double[] myList; // 首选的方法
double myList2[]; // 效果相同,但不是首选方法
myList = new double[8];
double[] arrayRefVar = new double[10];
double[] arrayRefVar2 = {1, 2, 3, 4};
arrayRefVar[0] = 5.6;
arrayRefVar[1] = 4.5;
arrayRefVar[2] = 3.3;
arrayRefVar[3] = 13.2;
arrayRefVar[4] = 4.0;
arrayRefVar[5] = 34.33;
arrayRefVar[6] = 34.0;
arrayRefVar[7] = 45.45;
arrayRefVar[8] = 99.993;
arrayRefVar[9] = 11123;
System.out.println("arrayRefVar数组:"+ Arrays.toString(arrayRefVar));
System.out.println("arrayRefVar2数组:"+Arrays.toString(arrayRefVar2));
// 打印所有数组元素
for (int i = 0; i < arrayRefVar.length; i++) {
System.out.println(arrayRefVar[i] + " ");
}
// 计算所有元素的总和
double total = 0;
for (int i = 0; i < arrayRefVar.length; i++) {
total += arrayRefVar[i];
}
System.out.println("数组总和:" + total);
// 查找最大元素
double max = arrayRefVar[0];
for (int i = 1; i < arrayRefVar.length; i++) {
if (arrayRefVar[i] > max) {
max = arrayRefVar[i];
}
}
System.out.println("最大值为: " + max);
}
}
step2: java 使用arraylist
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Lists {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Weibo");
// sites.clear(); 清除所有元素
int si = sites.size();
String name = sites.get(0);
sites.add("韩工");
sites.remove(0);
System.out.println(name);
System.out.println(si);
System.out.println(sites);
}
}
step3: java 使用hashmap
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class Mpas {
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<Integer, String> maps = new HashMap<Integer, String>();
// 添加键值对
maps.put(1, "Google");
maps.put(2, "Runoob");
maps.put(3, "Taobao");
maps.put(4, "Zhihu");
// maps.clear(); 清除所有
int si = maps.size();
String name = maps.get(3);
boolean isNum = maps.isEmpty();
System.out.println(isNum);
System.out.println(name);
System.out.println(si);
System.out.println(maps);
}
}
end