我想出了以下几点:
public static void cutString(String s) {
List<String> strings = new ArrayList<>();
int index = 0;
while (index < s.length()) {
strings.add(s.substring(index, Math.min(index + 1048576, s.length())));
index += 1048576;
}
}
Run Code Online (Sandbox Code Playgroud)
但我的问题是,使用 UTF-8 某些字符并不完全占用 1 个字节,因此使用 1048576 来告诉在哪里剪切字符串不起作用。我正在考虑使用迭代器,但这似乎效率不高。什么是最有效的解决方案?字符串可以小于 1 Mb 以避免字符切片,但不能大于 1 Mb!
我有一个包含项目名称的列表:
my_list = ['a', 'b', 'c', 'a', 'd', 'a', 'a']
我想将这些字母放入一个dictonary,其中键值包含数字,列表中的字母数量是多少:
my_dict = {'a' : 4, 'b' : 1, 'c' : 1, 'd' : 1}
Run Code Online (Sandbox Code Playgroud)
我怎么能在python中这样做?
我想基于Position枚举类型创建100个新实例.在switch case语句中我有5个案例,并且基于enum类型我想创建相应的Employee实例.我的问题是,我不知道如何在每次创建新实例时创建和分配新的变量名,例如:worker1,worker2,worker3.这是我到目前为止所得到的:
final String randomVariableName() {
int count = 1;
String s = "worker" + count;
count++;
return s;
}
final Position randomPosition(){
return positions[random.nextInt(positions.length)];
}
public void run() {
for (int i = 1; i <= 100; i++) {
String randomName = "worker" + i;
Position p = randomPosition();
switch (p) {
case PROJECT_LEADER:
ProjectLeader randomVariableName() = new ProjectLeader(p, randomName, 5000);
case DEVELOPER_LEADER:
DeveloperLeader randomVariableName() = new DeveloperLeader(p, randomName, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
但是这样,我无法调用该randomVariableName()方法,因为: …