假设我有以下代码:
String a = " some texte";
String b = " text";
String c = "sf ";
String d = " kjel";
String e = "lkjl";
ArrayList<String> list = new ArrayList<String>();
// better way to do all these adds without having to type them all?
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(e);
Run Code Online (Sandbox Code Playgroud)
如何使打字和计算更有效?
我得到这个代码的java.lang.IndexOutOfBoundsException.
private List<Integer> LoeschenX = new ArrayList<Integer>();
private List<Integer> LoeschenY = new ArrayList<Integer>();
for (int i : LoeschenY) LoeschenX.add(LoeschenY.get(i));
Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList包含对其父对象的引用的对象.
我试图删除特定父对象中的每个对象(所以等于父对象).
如果我做:
System.out.println(parent);
Run Code Online (Sandbox Code Playgroud)
我的控制台输出:
ParentObject@1f8166e5
Run Code Online (Sandbox Code Playgroud)
如果我做:
for(int i = 0; i < array.size(); i++){
if(array.get(i).getParent().equals(parent)){
array.remove(i);
}
}
Run Code Online (Sandbox Code Playgroud)
并且(测试)
for(int i = 0; i < array.size(); i++){
if(array.get(i).getParent().equals(parent)){
System.out.println(parent + ":" + array.get(i).getParent());
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制台输出如下:
ParentObject@1f8166e5:ParentObject@1f8166e5
Run Code Online (Sandbox Code Playgroud)
上面的代码片有什么问题?为什么array.remove(i)不工作?
我有一个整数的ArrayList.
例如,假设一个具有整数0,4,7,10的ArrayList
我现在想比较任何给定的整数,看看它在ArrayList上的位置.
所以例如,如果我的整数是3,它应该在0和4之间,所以结果将是1.如果我的整数是9,它将在7和10之间,所以我的结果将是3.
显然我可以循环遍历整个ArrayList,如下所示:
int indexOfItem = 0;
if (mylist.size()>0)
while (indexOfItem<mylist.size() && integerImSearching>mylist.get(itemsToSkip)) {
indexOfItem++;
}
Run Code Online (Sandbox Code Playgroud)
我可以从这个方法中获取我的项目的索引.但是这很好,如果ArrayList很小,我担心如果列表相当大,它不是最佳方式.有没有更好的方法来实现这一目标?
我有一个存储在ArrayList中的一些命令的列表,我一直试图将这个列表打印在一个文本文件中.以下是代码 -
try {
FileWriter writer = new FileWriter("Commands.txt");
for(String str: commandArray){
writer.write(commandArray[i]);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我没有错误,但也没有创建文件.有没有我做错了.任何帮助表示赞赏.非常感谢.
编辑问题
@Sajal Dutta,我接受了你的建议并想出了以下内容;
try {
for(String str: movementArray){
File file = new File("D://filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str);
bw.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
在创建文件的那一刻,但只有arraylist的最后一个值被打印到文件中.arraylist中存储了六个值,如何将所有六个值打印出来?
我第一次出现这种"奇怪"的情况.我需要为我的类创建两个不同的构造函数:
public OpponentListAdapter(Context c, ArrayList<MyCustomObject> l){}
Run Code Online (Sandbox Code Playgroud)
和
public OpponentListAdapter(Context c, ArrayList<String> l){}
Run Code Online (Sandbox Code Playgroud)
因为根据ArrayList的泛型类型,我需要执行不同的操作.但我有这个错误:
方法OpponentListAdapter(Context,ArrayList)具有相同的擦除> OpponentListAdapter(Context,ArrayList)作为OpponentListAdapter类型中的另一种方法
怎么了?也许解决方案很简单,但就目前而言,我找不到任何好处!
我无法弄清楚为什么for循环计数器在我想要之前/在它执行整个for循环块之前增加.我已经用打印语句乱码代码进行调试,这里是代码:
public void findSubSeq() {
temp = new ArrayList<Integer>();
temp.add(vals.get(0));
for (int i = 0; i < (vals.size()-2); i++) {
System.out.println(i + " index in array, for");
if (vals.get(i) < vals.get(i++)) {
System.out.println(i + " index in array, if 1");
temp.add(vals.get(i++));
System.out.println(i + " index in array, if 2");
System.out.println(this.getlargest());
}
else {
System.out.println(i + " index in array, else 1");
compareALs();
System.out.println(i + " index in array, else 2");
temp.add(vals.get(i++));
System.out.println(i + " index in array, else 3");
System.out.println(this.getlargest()); …Run Code Online (Sandbox Code Playgroud) 在我的代码中:
Collection<String> c = new ArrayList<>();
Iterator<String> it = c.iterator();
c.add("Hello");
System.out.println(it.next());
Run Code Online (Sandbox Code Playgroud)
发生异常,因为我的集合在创建迭代器后发生了变化.
但是在这段代码中呢:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
for (Integer integer : list) { // Exception is here
if (integer.equals(2)) {
list.remove(integer);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么发生例外?
在第二个代码中,我在for-each循环之前对我的集合进行了更改.
我已经学会了如何创建对象的Arraylist,这样的数组本质上是动态的.例如,要创建一个包含3个字段的对象数组(类Matrices的实例),代码如下所示:
ArrayList<Matrices> list = new ArrayList<Matrices>();
list.add( new Matrices(1,1,10) );
list.add( new Matrices(1,2,20) );
Run Code Online (Sandbox Code Playgroud)
此外,Matrices课程如下:
public class Matrices{
int x;
int y;
int z;
Matrices(int x,int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如何从该数组名称访问任何元素的每个字段list?特别是,如何20从该数组的第二个元素访问该字段,其值为(1,2,20)?
嗨,我想找到一个类类型的数组列表的重复对象.我尝试使用哈希集但不起作用.有人可以帮忙吗
package stream;
import java.util.*;
public class Chumma {
public static void main(String[] args) {
Dummy d = new Dummy();
Dummy d1 = new Dummy();
Dummy d2 = new Dummy();
d.setAge(14);
d.setName("XXX");
d1.setAge(15);
d1.setName("YYY");
d2.setAge(14);
d2.setName("XXX");
List<Dummy> list = new ArrayList<Dummy>();
list.add(d);
list.add(d1);
list.add(d2);
Set<Dummy> uniqueSet = new HashSet<Dummy>(list);
Set uniqueEntries = new HashSet<Dummy>();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (!uniqueEntries.add(element)) // if current element is a duplicate,
// iter.remove();
System.out.println(iter.toString());
}
} …Run Code Online (Sandbox Code Playgroud) arraylist ×10
java ×10
android ×2
collections ×2
arrays ×1
exception ×1
filewriter ×1
for-loop ×1
generics ×1
indexing ×1