请问怎么优化以下代码 for (int i1 = 0; i1 < list1.size(); i1++) { String s = (String) list1.get(i1); if(s.contains("1")) { s = s.replace("1", "a"); } else if(s.contains("2")) { s = s.replace("2", "b"); } else if(s.contains("3")) { s = s.replace("3", "c"); } else if(s.contains("4")) { s = s.replace("4", "d"); } list1.remove(i1); list1.add(i1, s); }
时间: 2023-03-30 13:01:55 浏览: 54
您好,以下是代码的优化建议:
for (int i1 = ; i1 < list1.size(); i1++) {
String s = list1.get(i1);
if(s.contains("1")) {
s = s.replace("1", "a");
} else if(s.contains("2")) {
s = s.replace("2", "b");
} else if(s.contains("3")) {
s = s.replace("3", "c");
} else if(s.contains("4")) {
s = s.replace("4", "d");
}
list1.set(i1, s);
}
希望对您有帮助。
相关问题
小明写了一段代码,想实现替换文件部分内容的目标,但是代码似乎写得有些问题,请你纠正并优化这段代码,并给出你的优化逻辑,优化点多多益善。 public static void main(String[] args) throws IOException { File file = new File("some file path"); List list1 = new ArrayList(); FileInputStream fileInputStream = new FileInputStream(file); byte[] bytes = new byte[1024]; int i = fileInputStream.read(bytes); if(i > 0) { do { i = fileInputStream.read(bytes); String s = new String(bytes, 0 , i); list1.add(s); } while(i != -1); } i = fileInputStream.read(bytes); String start = new String(bytes, 0, i); list1.add(start); for (int i1 = 0; i1 < list1.size(); i1++) { String s = (String) list1.get(i1); if(s.contains("1")) { s = s.replace("1", "a"); } else if(s.contains("2")) { s = s.replace("2", "b"); } else if(s.contains("3")) { s = s.replace("3", "c"); } else if(s.contains("4")) { s = s.replace("4", "d"); } list1.remove(i1); list1.add(i1, s); } FileOutputStream fos = new FileOutputStream(file); for (Object o : list1) { fos.write(o.toString().getBytes()); } }
public static void main(String[] args) throws IOException {
File file = new File("some file path");
List<String> list1 = new ArrayList<>(); // 使用泛型
FileInputStream fileInputStream = new FileInputStream(file);
int i = 0;
byte[] bytes = new byte[1024];
while ((i = fileInputStream.read(bytes)) != -1) { // 使用while循环读取文件
String s = new String(bytes, 0, i);
list1.add(s);
}
fileInputStream.close(); // 关闭输入流
String start = list1.get(0); // 第一项即为文件内容
list1.remove(0); // 移除第一项
for (int i1 = 0; i1 < list1.size(); i1++) { // 循环遍历list1
String s = list1.get(i1);
if (s.contains("1")) {
s = s.replace("1", "a");
} else if (s.contains("2")) {
s = s.replace("2", "b");
} else if (s.contains("3")) {
s = s.replace("3", "c");
} else if (s.contains("4")) {
s = s.replace("4", "d");
}
list1.set(i1, s); // 直接使用set方法替换原来的元素
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(start.getBytes()); // 先写入原来的文件内容
for (String s : list1) { // 循环遍历list1写入修改后的内容
fos.write(s.getBytes());
}
fos.close(); // 关闭输出流
}
优化逻辑:
1. 使用泛型,避免了类型转换;
2. 使用while循环读取文件,避免了重复读取;
3. 关闭输入流和输出流,避免资源泄漏;
4. 直接使用set方法替换原来的元素,避免了移除和添加的操作;
5. 先写入原来的文件内容,再写入修改后的内容,避免了覆盖原来的文件内容。
贪心算法解决背包问题的思路和java代码
贪心算法是一种基于贪心思想的算法,它每次选取当前状态下最优的解,从而得到全局最优解。在背包问题中,可以使用贪心算法来求解最优解。
以下是贪心算法解决背包问题的基本思路:
1. 计算每个物品的单位价值,即价值除以重量。
2. 按照单位价值从大到小排序。
3. 依次选取单位价值最大的物品放入背包中,直到背包装满或所有物品都被选取。
以下是一个简单的 Java 代码实现:
```java
import java.util.*;
public class Knapsack {
// 背包容量和物品价值、重量
private int capacity;
private int[] values;
private int[] weights;
public Knapsack(int capacity, int[] values, int[] weights) {
this.capacity = capacity;
this.values = values;
this.weights = weights;
}
// 计算每个物品的单位价值
private double[] computeUnitValues() {
double[] unitValues = new double[values.length];
for (int i = 0; i < values.length; i++) {
unitValues[i] = (double) values[i] / weights[i];
}
return unitValues;
}
// 使用贪心算法求解背包问题
public int greedyAlgorithm() {
int totalValue = 0;
double[] unitValues = computeUnitValues();
List<Integer> items = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
items.add(i);
}
items.sort((i1, i2) -> Double.compare(unitValues[i2], unitValues[i1]));
for (int i = 0; i < items.size(); i++) {
int index = items.get(i);
if (weights[index] <= capacity) {
totalValue += values[index];
capacity -= weights[index];
} else {
totalValue += (int) (capacity * unitValues[index]);
break;
}
}
return totalValue;
}
public static void main(String[] args) {
int capacity = 50;
int[] values = {60, 100, 120, 140, 180};
int[] weights = {10, 20, 30, 40, 50};
Knapsack knapsack = new Knapsack(capacity, values, weights);
int result = knapsack.greedyAlgorithm();
System.out.println("最大价值:" + result);
}
}
```
在上面的代码中,我们设置了背包容量为50,共有5个物品,其价值和重量分别存储在 `values` 和 `weights` 数组中。`computeUnitValues` 函数用于计算每个物品的单位价值。`greedyAlgorithm` 函数用于使用贪心算法求解背包问题。我们先计算出每个物品的单位价值,然后按照单位价值从大到小对物品进行排序。依次选取单位价值最大的物品放入背包中,直到背包装满或所有物品都被选取。最后输出求解得到的最大价值。
需要注意的是,贪心算法并不一定能够求得最优解,但在一些特殊情况下,它可以得到全局最优解。如果希望得到更加精确的结果,建议使用动态规划算法或者遗传算法等更加复杂的算法。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)