我有许多函数将全局ArrayList作为参数,其中一些不对此列表进行任何更改,而其他函数需要在工作时删除此数组的某些元素,因此我在这些函数中创建了一个本地tempArrays.
static ArrayList array1 = new ArrayList();
public fn1(ArrayList array1)
{
ArrayList tempArray1 = new ArrayList();
tempArray1 = array1;
tempArray1.remove(elemnt);
}
Run Code Online (Sandbox Code Playgroud)
问题是删除的元素也从原始arrayList中删除array1,我不知道为什么?.
谢谢..
这与类中的对象数组有关.我最初将它添加到该问题,但因为这个问题得到了回答,我没有收到回复.
所以我有2节课.一个包含另一个类的列表.我正在将数据添加到我从数据库中取出的类中.但是,当我从List中取出数据时,值都是相同的.它们是列表中最后一个元素的值.
我已经尝试在每个循环中重新创建对象,但它似乎仍然导致问题.我不知道我在这里做错了什么.
public class xmldata {
String Barcode;
String First;
String Last;
String Phone;
String Email;
String md5sum;
String zipfile;
List<PictureData> pics = new ArrayList<PictureData>();
Run Code Online (Sandbox Code Playgroud)
...
public class PictureData {
static String filename;
static String directory;
Run Code Online (Sandbox Code Playgroud)
...
xmldata data = new xmldata();
ResultSet pictures=db.query("select * from pictures where barcode=?",barcode);
while (pictures.next()) {
PictureData pictemp= new PictureData();
pictemp.setdirectory(pictures.getString("path"));
pictemp.setfilename(pictures.getString("filename"));
data.pics.add(pictemp);
}
Run Code Online (Sandbox Code Playgroud)
...
for (int j=0; j<data.pics.size();++j) {
String path;
PictureData pictemp2= new PictureData();
pictemp2=(PictureData) data.pics.get(j);
path=pictemp2.getdirectory()+pictemp2.getfilename();
System.out.println(path);
zip.addfile(path); …Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList<String>,我需要传递此ArrayList中的每个String,作为此函数的参数:
protected Void myFunction(String... params)
Run Code Online (Sandbox Code Playgroud)
注意:我无法修改 myFunction
我有一个奇怪的问题,我知道可以解决的方法,但是这次我想用数组列表来解决。问题出在这里:我有一棵员工树。Employee是一个简单的类(在为此雇员工作的雇员列表下):
class Employee
{
String name;
ArrayList<Employee> under = new ArrayList<Employee>();
//fire function
}
Run Code Online (Sandbox Code Playgroud)
我的任务是递归解雇所有没有雇员的雇员。我知道如何使用自定义列表数据结构来解决此问题,但是我想使用数组列表来做到这一点。到目前为止,这是我的代码:
public boolean Fire()
{
if (under.isEmpty())
return true;
else
{
for (int x = 0; x < under.size(); x ++)
{
if (under.get(x).Fire())
under.remove(x);
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但是此代码的问题是,当我删除under.remove(x)时,under.size()会变小,索引会变得混乱。我尝试在每个under.remove(x)之后设置x = 0,但它做得并不正确。仍有很多员工离职。有数组列表结构的解决方案吗?
我有一个ArrayList,我正在填写我打印的东西,以确保我不重复相同的字符串.我正在使用.contains()方法来检查:
if(!outputted.contains(string)){etc...}
Run Code Online (Sandbox Code Playgroud)
我在Eclipse上运行调试器仍然打印重复,我看到即使ArrayList确实持有String,它仍然会读取if语句,就好像它没有.我真的很困惑为什么会这样.任何提示或建议将不胜感激.
我在这里有这个代码(使用lwjgl但是应该没有意义)尝试在按下esc键时暂停游戏.我使用带有键的ArrayList来跟踪按下的内容和不按下的内容.
public List<Integer> keys = new ArrayList<Integer>();
public void get() {
if (isKeyDown(KEY_ESCAPE) && !keys.contains(KEY_ESCAPE)) {
keys.add(KEY_ESCAPE);
keyEscape();
}
}
public void rem() {
if (!isKeyDown(KEY_ESCAPE) && keys.contains(KEY_ESCAPE))
keys.remove(KEY_ESCAPE);
}
private void keyEscape() {
Screen.paused ^= true;
}
Run Code Online (Sandbox Code Playgroud)
这是由循环,这不叫get()和rem()之后另一个一环,在这个顺序.这给了我一个真棒java.lang.IndexOutOfBoundsException: Index: 1, Size: 1的keys.remove(KEY_ESCAPE);时候我放手ESC的.
有谁有分享的见解?
我一直在我的项目中使用ArrayLists,我需要创建一个默认的ArrayList,这样我就可以随时重置原始的ArrayList.因此,我复制原始的ArrayList以创建默认的ArrayList.但是,每当我修改原始内容时,它也会更改默认值.如何使副本"静态"且不可更改?
这是我的代码:(用葡萄牙语)
private ArrayList<Compartimento> listaCompartimentos;
private ArrayList<Compartimento> listaCompartimentosDEFAULT;
public Simulador() {
this.listaCompartimentos = new ArrayList<>();
this.listaCompartimentosDEFAULT=new ArrayList<>();
}
//Copy of the array
public void gravarListaDefault(){
this.listaCompartimentosDEFAULT=(ArrayList<Compartimento>)listaCompartimentos.clone();
}
Run Code Online (Sandbox Code Playgroud)
注意:我不知道它背后的原因是否可能,但ArrayList listaCompartimentos有一个listaEquipamentos.对于每个"Compartimento",都有一个ArrayList"listaEquipamentos".
public class Tabel {
private static int dimension;
private ArrayList<ArrayList<Character>> tabel;
public Tabel(int dimension) {
Tabel.dimension = dimension;
for (int i=0;i<Tabel.dimension*Tabel.dimension;i++) {
tabel.add(new ArrayList<Character>());
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试调试(eclipse ide)时,我会遇到很多奇怪的"错误",或者至少我会遇到一些我认为意想不到的错误.
private static int不会出现在debug的"variables"部分.
我得到一个NullPointerException上tabel.add(...),但是当我观看了调试,它进入for一次,在表中不添加任何东西,因为当我点击"下一步",而不是跳转到花括号它跳出功能.
如果我发表评论,.add那就是问题(我认为).我的语法错了吗?或者我应该发布更多代码?
我在Map中存储了一个ArrayList,我正在尝试将这个ArrayList返回:
//init map with data: Map<Integer, ArrayList<MyObject>>
while(iterator.hasNext()) {
Entry entry = iterator.next();
ArrayList value = (ArrayList)entry.getValue();
}
Run Code Online (Sandbox Code Playgroud)
将entry.getValue()转换为一般的ArrayList似乎可行,但是当我尝试将其强制转换为特定的ArrayList时:
ArrayList<MyObject> value = (ArrayList<MyObject>)entry.getValue();
Run Code Online (Sandbox Code Playgroud)
编译器通过错误.某种"上次编辑警告".是否可以将它转换为我的特定ArrayList,以便我可以在之后使用For-Each-Loop循环它?
我试图将CD对象添加到CD的ArrayList的Band Object的ArrayList成员字段中.band_index是Band ArrayList的索引,当它从组合框中选择时,我已经检查了band_index确实分配了所选波段的正确索引.band.get(band_index).addCD(cd);当我去调用当前Band的addCD方法时,我在这行代码上得到一个Null Pointer Exception .
主要课程:
public void addCD() {
CD cd = new CD(t, y);
band.get(band_index).addCD(cd); //NULL pointer Exception on this line
updateCDs();
}
//Method to print out all the CDs of a band
public void updateCDs() {
String list = "";
for(int i = 0; i < band.size(); i++)
{
//band_index is the index of the selected band in the combobox
if(i == band_index) {
for(int j = 0; j < band.get(i).getCDs().size(); j++) {
list …Run Code Online (Sandbox Code Playgroud)