我想知道是否可以将s 的Java转换List为Strings数组String:
我试过这个:
List<String> products = new ArrayList<String>();
//some codes..
String[] arrayCategories = (String[]) products.toArray();
Run Code Online (Sandbox Code Playgroud)
但它给了我一个例外信息:
java.lang.ClassCastException:java.lang.Object []无法强制转换为java.lang.String []
我需要将给定数字的ArrayList的某些部分的值递增.
比如[ 1 2 3 4 5 6 7 8 9 1 1 1 ]
让我说我需要将最后3个元素增加2,以便结果如此
[ 1 2 3 4 5 6 7 8 9 3 3 3 ]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点 ?
有点奇怪.我正在尝试创建一个棋盘游戏(步步高)并创建了一个名为piece的对象.在我的板上,我正在尝试创建一个数组列表
ArrayList<piece> pieces = new ArrayList<piece>();
piece newPiece = new piece(1, 1, "red", "top");
piece newPiece2 = new piece(1, 2, "black", "top");
pieces.add(newPiece);
System.out.println(newPiece.getCol());
pieces.add(newPiece2);
System.out.println(newPiece2.getCol());
Run Code Online (Sandbox Code Playgroud)
我期待输出为"红色"然后"黑色",但我实际上是"黑色"然后"黑色"?有什么建议为什么会这样?
我的问题是这个
Scanner sf = new Scanner(f);
ArrayList<String> teamArr = new ArrayList<String>();
int counterPopulate = 0;
while(sf.hasNextLine()){
teamArr[counterPopulate] = sf.nextLine();
counterPopulate++;
}
Run Code Online (Sandbox Code Playgroud)
任何解决方案,这是一个尝试捕获包围.在这一部分得到问题teamArr[counterPopulate] = sf.nextLine();
我有一个ArrayList,我想开始迭代它,比如说索引100到最后.我怎么做?
帮助从ArrayList获取值
data = new ArrayList<Map<String, String>>();
for (int i = 0; i < marks.length; i++) {
HashMap<String, String> m = new HashMap<String, String>();
m.put("title", marks[i].getUserName());
m.put("mark", marks[i].getMark().toString());
m.put("type", marks[i].getType().toString());
m.put("description", marks[i].getDescription());
data.add(m);
}
Run Code Online (Sandbox Code Playgroud)
试图从适配器中取出一个ArrayList并将值写入数组:
data = simpleAdapter.getData();
MarksRead[] marksReads = new MarksRead[data.size()];
for (int i = 0; i < data.size(); i++) {
marksReads[i].setDescription(data(i).get("description"));
marksReads[i].setMark(Integer.valueOf(data.get(i).get("mark")));
marksReads[i].setType(Integer.valueOf(data.get(i).get("type")));
}
Run Code Online (Sandbox Code Playgroud)
在线marksReads[i].setDescription(data(i).get("description"));输出是Exception NullPointerException.
帮忙,怎么做对.
有人能告诉我下面的代码有什么问题吗?当我在try之后运行第一行下面的代码时{产生以下错误:
"java.lang.IndexOutOfBoundsException:Index:0,Size:0"
public static class ASIFFile {
private ArrayList<HashMap<String,String>> data;
private static int currRec = 0; //assign each record a numeric id based on this figure.
// Method for reading ADIFfile
public ArrayList<HashMap<String,String>> ReadASIFfile (File DataFile) {
data = new ArrayList<HashMap<String, String>>(500);
try {
HashMap<String, String> temp = new HashMap<String,String>(10);
data.set(currRec, temp);
(data.get(currRec)).put("recID", Integer.toString(currRec));//give the record a numeric ID
...
Run Code Online (Sandbox Code Playgroud) 众所周知,如果我想查找对象是否在arraylist中,我可以使用contains()方法:
if(arraylist.contains(obj)) { // do things }
Run Code Online (Sandbox Code Playgroud)
或者我可以使用for循环(假设我已经重载了equals()方法):
for(Object o : arraylist) {
if(obj.equals(o)) {
// do things
}
}
Run Code Online (Sandbox Code Playgroud)
正如在其他帖子中提到的那样,contains()实际上在for循环和equals()方法的内部使用.因此,我的问题是:当arraylist更大时,期望contains()花费更多时间是合乎逻辑的吗?
我问这个是因为在我的代码中我使用contains()以避免"for循环"并因此保持运行时间低且恒定,但我注意到随着arraylist大小变大,代码运行速度明显变慢.
我有一个由字符串组成的ArrayList.这个ArrayList包含......
"60分钟""120分钟""30分钟"
如果我使用Collections.sort(ArrayList);它首先导致"120分钟".这是因为"120"中的"1"明显小于"30"中的"3"
有没有一种简单的方法可以对这个ArrayList进行排序,使其显示为......
"30分钟""60分钟""120分钟"
今天我有一个比昨天更愚蠢的问题,但我真的不明白为什么我的代码不起作用.
代码如下:
public class A{
List fileList;
public A(List fl){
fileList = new ArrayList<String>;
if(fl != null) {
fileList = fl;
}
}
public void removeFiles(List fl){
for(Object obj : fl) {
fileList.remove(obj.toString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了在网络上发现的每一件事,即使有一个Iterator但没有文件被删除,也没有出现错误或异常!
一些更多的代码
public class CoordinationServer extends Activatable implements CoordinationSerInt
{
List<CoordObj> userList;
public int updateFileList(String username, List files, int option) throws RemoteException{
for (CoordObj anUserList : userList) {
if (anUserList.getUsername().equals(username)) {
if(option == 0){
anUserList.setFileList(files);
return 1;
}else if(option == 1){
anUserList.addFilesToList(files); …Run Code Online (Sandbox Code Playgroud)