2020-11-07

升序
引入工具
导入工具类

import 工具类的路径

代码

import java.util.Arrays;
使用工具
sort方法

功能

帮我们把数组进行升序,由小到大
会影响数组内部的结构
用法

Arrays.sort(数组);
代码
import java.util.Arrays;

public class Test {
public static void main(String[] args) {
int[] nums = new int[]{2,5,3,2,6};

Arrays.sort(nums);

for(int i=0;i<nums.length;i++){
    System.out.println(nums[i]);
}

}
}

交换两个变量
使用第三个变量
代码

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
int a = 11;
int b = 22;
int temp;

System.out.println("数据交换前a与b的值" + a + "---" + b);
// 让临时的变量接收一个数据
temp = b;
b = a;
a = temp;

// 数据交换成功
System.out.println("数据交换后a与b的值" + a + "---" + b);

}
}

核心代码

temp = b;
b = a;
a = temp;

不允许使用第三个变量
核心代码

a=a+b;
b=a-b;
a=a-b;

逆序
光标移动的最大下标算法
数组会在哪里停下,取决于数组的长度。
如果数组的长度是n,那么
下标不会走到n/2
代码
public class Test {
public static void main(String[] args) {
// 初始化一个数组
String[] strList = {“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”};

// 遍历时遇到哪个索引会停止的算法: 数组的长度除2取整
int topIdx = strList.length / 2;

// 开始遍历,让下标从0开始游走
for (int i = 0; i < topIdx; i++) {
    // 把下标所在的值交给临时变量
    String temp = strList[i];
    // 获得交换索引值,计算出要与哪个索引进行数据交换
    int changeIndex = strList.length - 1 - i;
    // 把交换索引值,对应的数据赋值给当前光标所在空间
    strList[i] = strList[changeIndex];
    // 把临时变量的值,赋值给交换索引的值
    strList[changeIndex] = temp;
}

// 遍历数组,查看排序后的效果
for (int j = 0; j < strList.length; j++) {
    System.out.print(strList[j]);
}

}

求最大值
算法
来一个临时变量
让数组成员的值与临时变量比较
谁大,就把谁的值赋给临时变量
代码
public class getmaxnum {
public static void main(String[] args) {
// 定义一个整数数组,同时给与初始值
int[] numList = {11, 22, 4, 3, 55, 33};

// 开始比较大小
// 定义一个变量,用于保存最大的数据
int temp = numList[0];

// 开始鹿历
for (int i = 1; i < numList.length; i++) {
    // 需要重复的事情就是
    // 拿temp的数据与下标所对应的数据进行大小比较,
    if (numList[i] > temp) {
        // 把当前下标对应的值,赋给temp变量
        temp = numList[i];
    }
}

System.out.println("最大的值是" + temp);

}
// 定义一个整数数组,同时给与初始值
int[] numList = {11, 22, 4, 3, 55, 33};

// 开始比较大小
// 定义一个变量,用于保存最大的数据
int temp = numList[0];

// 开始鹿历
for (int i = 1; i < numList.length; i++) {
    // 需要重复的事情就是
    // 拿temp的数据与下标所对应的数据进行大小比较,
    if (numList[i] > temp) {
        // 把当前下标对应的值,赋给temp变量
        temp = numList[i];
    }
}

System.out.println("最大的值是" + temp);

}
}

追加数据
算法
遍历数组找到第一个出现null的位置
记录这个位置,并往数组的这个位置插入数据
代码
String[] strList = new String[5];

// 给空间赋值
strList[0] = "hello";
strList[1] = "java";
strList[2] = "welcom";

// 需要插入的数据
String sendKey = "c#";

// 插入算法
for (int i = 0; i < strList.length; i++) {
    System.out.println(i);
    // 每一次进入循环要重复做的事情
    // 判断光标i所对应的值是否为null
    if (strList[i] == null) {

// 给空间赋值
strList[0] = “hello”;
strList[1] = “java”;
strList[2] = “welcom”;

// 需要插入的数据
String sendKey = "c#";

// 插入算法
for (int i = 0; i < strList.length; i++) {
    System.out.println(i);
    // 每一次进入循环要重复做的事情
    // 判断光标i所对应的值是否为null
    if (strList[i] == null) {

// System.out.println(“光标对应的值是null” + i);
strList[i] = sendKey;
break;
}
}

for (int j = 0; j < strList.length; j++) {
    System.out.println(strList[j]);
}

for (int j = 0; j < strList.length; j++) {
System.out.println(strList[j]);
}
中部插入数据
伪代码

第一步:
找到最大有效数据的下标
int temp=0;
for(int m=0;m<数组.length;m++){
if(数组[m]==null){
temp = m;
break;
}
temp–;

}

第二步:后移
// 数据后移的遍历
for(int i=temp;i>j;i–){
数组[i+1]=数组[i];
}

第三步:
数据的插入
数组[j]=“html";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Java 中的 LocalDateTime 类来处理时间。 首先,我们需要将给定的时间段转换为 LocalDateTime 对象: ```java LocalDateTime start = LocalDateTime.of(2020, 11, 20, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2021, 10, 9, 23, 59, 59); ``` 这里将结束时间设置为 23:59:59 是因为我们想要包含该日期的所有时间。 接下来,我们可以使用一个循环来遍历时间段中的每一天,并获取该天的开始时间和结束时间: ```java LocalDateTime current = start; while (!current.isAfter(end)) { LocalDateTime dayStart = current.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime dayEnd = current.withHour(23).withMinute(59).withSecond(59).withNano(999999999); System.out.println("Day " + current.toLocalDate() + " starts at " + dayStart + " and ends at " + dayEnd); current = current.plusDays(1); } ``` 这里,我们使用 `withHour`、`withMinute`、`withSecond` 和 `withNano` 方法来设置每天的开始时间和结束时间。然后,我们可以使用 `plusDays` 方法来增加 `current` 对象的日期,以便遍历整个时间段。 完整代码如下: ```java import java.time.LocalDateTime; public class Main { public static void main(String[] args) { LocalDateTime start = LocalDateTime.of(2020, 11, 20, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2021, 10, 9, 23, 59, 59); LocalDateTime current = start; while (!current.isAfter(end)) { LocalDateTime dayStart = current.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime dayEnd = current.withHour(23).withMinute(59).withSecond(59).withNano(999999999); System.out.println("Day " + current.toLocalDate() + " starts at " + dayStart + " and ends at " + dayEnd); current = current.plusDays(1); } } } ``` 输出结果如下: ``` Day 2020-11-20 starts at 2020-11-20T00:00 and ends at 2020-11-20T23:59:59.999999999 Day 2020-11-21 starts at 2020-11-21T00:00 and ends at 2020-11-21T23:59:59.999999999 Day 2020-11-22 starts at 2020-11-22T00:00 and ends at 2020-11-22T23:59:59.999999999 ... Day 2021-10-07 starts at 2021-10-07T00:00 and ends at 2021-10-07T23:59:59.999999999 Day 2021-10-08 starts at 2021-10-08T00:00 and ends at 2021-10-08T23:59:59.999999999 Day 2021-10-09 starts at 2021-10-09T00:00 and ends at 2021-10-09T23:59:59.999999999 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值