import java.util.ArrayList;
import java.util.Collections;
public class SmartCombining {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
Collections.addAll(list1, 4, 3);
Collections.addAll(list2, 5, 10, 4, 3, 7);
smartCombine(list1, list2);
System.out.println(list1);
System.out.println(list2);
}
public static void smartCombine(ArrayList<Integer> first,
ArrayList<Integer> second) {
first.addAll(second);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我想将两个列表合并为一个,但如果第二个列表包含第一个列表,则不会添加.到目前为止,我的方法将它们加在一起.
import java.util.ArrayList;
public class Variance {
// Copy here sum from exercise 63
public static int sum(ArrayList<Integer> list) {
int sum = 0;
for(int i=0; i<list.size(); i++ ){
sum = sum + list.get(i) ;
}
return sum;
}
// Copy here average from exercise 64
public static double average(ArrayList<Integer> list) {
double average = sum(list)/list.size();
return average;
}
public static double variance(ArrayList<Integer> list) {
// write code here
double sumMinusAverage = sum(list) - average(list);
return sumMinusAverage * sumMinusAverage / (list.size()-1); …Run Code Online (Sandbox Code Playgroud) import java.util.Random;
public class PasswordRandomizer {
// Define the variables
private Random random = new Random();
private int passwordLength;
private String password = "";
public PasswordRandomizer(int length) {
// Initialize the variable
this.passwordLength = length;
}
public String createPassword() {
// write code that returns a randomized password
for(int i = 0; i < this.passwordLength; i++){
int j = random.nextInt();
char symbol = "abcdefghijklmnopqrstuvwxyz".charAt(j);
this.password = this.password + symbol;
}
return this.password;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在字符串中添加字符,我试过这个,但是我收到了这个错误:
"线程中的异常"主"java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-414383904".