我有一个字符串,比如说a="1.1"和一个arraylist,比如说list,它有字符串:"1.1.1.1", "1.1.2.4","1.2,1.3","1.5.1.1"
现在,我想从这个arraylist中获取字符串,它在开头包含字符串a ="1.1",这意味着a是这些字符串的子字符串,需要从头开始匹配.
因此,对于这种情况,答案将是1.1.1.1,1.1.2.4但不是1.5.1.1这里1.1不是在开头.
我知道,如何实现这一点,但我认为我的解决方案效率不高,对于大型arraylist,它需要更多的处理时间.
我的方法: 为arraylist和每个字符串运行for循环,从头开始用a的长度裁剪字符串,并检查裁剪的字符串是否等于a.
但是如果我想为一个大型的arraylist重复几个字符串,我认为这不是一个好的解决方案.
任何的想法?我将非常感谢你的帮助.
我有一个如此定义的列表
List<String[]> output = new ArrayList<String[]>();
Run Code Online (Sandbox Code Playgroud)
将值添加到其中:
String[] link = new String[3];
link[0] = page
link[1] = title
link[2] = pagerank
output.set(i, link);
Run Code Online (Sandbox Code Playgroud)
根据" pagerank " 降序对" 链接 "数组进行重新排序的最简单方法是什么?
我arrayList [5,8,13,18,19]从服务器获取,我想RadioGroup在xml文件中创建一个.选择可解决的项目后,我会将所选项目放入arrayList并在单击确定按钮后将查询发送到服务器.我怎样才能以编程方式创建RadioGroup?
我试过这个,但我不知道如何设置值RadioButton与ArrayList值.我怎样才能让tp工作?
private void createRadioButton(final ArrayList<Integer> items) {
final LinearLayout ll = (LinearLayout) findViewById(R.id.lila);
final ArrayList<RadioButton> rb = new ArrayList<RadioButton>();
final RadioGroup rg = new RadioGroup(this); // create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL
for (int i = 0; i < items.size(); i++) {
items.get(i) = new RadioButton(this);
}
}
Run Code Online (Sandbox Code Playgroud) 我的问题:
al.get(i).setName()=arr[i];
Run Code Online (Sandbox Code Playgroud)
这被认为是无效的,因为左侧应该只包含一项任务.那么如何存储价值呢?
我试图用来ArrayList<ArrayList<String>>解析网页中的一些片段,但我的代码中出了问题:
ArrayList<ArrayList<String>> x = new ArrayList<ArrayList<String>>();
ArrayList<String> s = new ArrayList<String>();
int z, y, w = 0;
for(z=0; z<3;z++)
{
s.clear();
for(y=0;y<4;y++)
{
s.add(""+w++);
}
x.add(s);
}
for(ArrayList<String> tr : x)
{
for( String t : tr)
{
System.out.println(t);
}
System.out.println("-----------------");
}
Run Code Online (Sandbox Code Playgroud)
输出是:
8
9
10
11
8
9
10
11
8
9
10
11
Run Code Online (Sandbox Code Playgroud)
而不是(我的预期输出):
0
1
2
3
4
5
6
7
8
9
10
11
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么会这样吗?谢谢.
我试图做一个程序,读取命名的文本文件text.txt到ArrayList.之后,它必须删除包含"我爱蛋糕"字样的任何文本行
所以说这是文件中的文字:
I love cake so much
yes i do
I love cake
I dont care
Run Code Online (Sandbox Code Playgroud)
这是我的代码.我有它阅读文件,但我不明白我如何删除某些行(包含"我爱蛋糕").
import java.io.*;
import java.util.*;
public class Cake {
public static void main(String[] args) throws Exception {
File fileIn = new File("text.txt");
ArrayList<String> text = new ArrayList<String>();
Scanner s= new Scanner(fileIn);
String line;
while (s.hasNextLine()) {
line = s.nextLine();
System.out.println(line);
}
s.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我的代码有一个奇怪的问题.
下面是我测试Chunk类的代码:
List<Chunk> chunks = new ArrayList<Chunk>();
chunks.add(new Chunk(1,1,1));
System.out.println(chunks.indexOf(new Vector3i(1, 1, 1)));
Run Code Online (Sandbox Code Playgroud)
这里是Chunk类的equals方法:
public boolean equals(Object object) {
System.out.println("Test _1_");
if (object != null && object instanceof Vector3i) {
System.out.println("Test _2_");
if((this.x == ((Vector3i) object).x)&&(this.y == ((Vector3i) object).y)&&(this.z == ((Vector3i) object).z)) {
System.out.println("Test _3_");
return true;
}
}
System.out.println("Test _4_");
return false;
}
Run Code Online (Sandbox Code Playgroud)
Vector3i:
public class Vector3i {
public int x;
public int y;
public int z;
public Vector3i(int x, int y, int z) {
this.x = x;
this.y …Run Code Online (Sandbox Code Playgroud) import java.util.ArrayList;
import java.util.List;
public class Apple {
private static String color;
private static int weight;
public Apple(String color, int weight){
this.color = color;
this.weight = weight;
}
public int getWeight(){
return weight;
}
public String getColor(){
return color;
}
public interface AppleFormatter{
String accept(Apple a);
}
public static class AppleFancyFormatter implements AppleFormatter{
public String accept(Apple apple){
String characteristic = apple.getWeight() > 150 ? "heavy" : "light";
return "A " + characteristic + " " + apple.getColor() + " apple"; …Run Code Online (Sandbox Code Playgroud) ArrayList < Integer > arraylist = new ArrayList < Integer > ();
arraylist.add(10010);
arraylist.add(5);
arraylist.add(4);
arraylist.add(2);
for (int i = 0; i < arraylist.size(); i++) {
for (int j = arraylist.size() - 1; j > i; j--) {
if (arraylist.get(i) > arraylist.get(j)) {
int tmp = arraylist.get(i);
arraylist.get(i) = arraylist.get(i);
arraylist.get(j) = tmp;
}
}
}
for (int i: arraylist) {
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
交换时出错,LHS应该是可变的.我明白了.Set方法在这里工作,但我不想使用.有没有办法不使用set方法呢?非常感谢帮助.
我有一个字符串的ArrayList,它存储我要打印到属性文件的值.我想在每行的末尾添加一个反斜杠,这样我就可以有多行值.要将值放在ArrayList中,我这样做:
arrayListName.add(value + '\\');
Run Code Online (Sandbox Code Playgroud)
它在我的ArrayList中产生"value \\",最终在文件中有四个反斜杠.这个:
arrayListName.add(value + '');
Run Code Online (Sandbox Code Playgroud)
只生成"值",因此没有附加反斜杠的值.我做错了什么?