标签: arraylist

我的ArrayList正在覆盖以前的数据

我正在尝试ArrayList用我的其他代码解决一些问题的简单示例,但这个简单的代码提供了错误的输出.

我创建了一个Hashmap的Arraylist,在该Hashmap中放入3个键/值对,然后将该Hashmap放在ArrayList中,就像这样,

public class SortData {

    public static void main (String [] args){

    ArrayList<HashMap<String, String>> myArrayList = new ArrayList<HashMap<String,String>>();

    HashMap<String, String> myHashMap = new HashMap<String, String>();


        myHashMap.put("title",  "first Title");
        myHashMap.put("date",   "This is date");
        myHashMap.put("number", "5");

        myArrayList.add(0, myHashMap);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在数组列表中添加更多数据时

import java.util.ArrayList;
import java.util.HashMap;

public class SortData {

    public static void main (String [] args){

    ArrayList<HashMap<String, String>> myArrayList = new ArrayList<HashMap<String,String>>();

    HashMap<String, String> myHashMap = new HashMap<String, String>();


        myHashMap.put("title",  "first Title");
        myHashMap.put("date",   "This is date");
        myHashMap.put("number", "5");

        myArrayList.add(0, myHashMap);


        myHashMap.put("title", …
Run Code Online (Sandbox Code Playgroud)

java arraylist

0
推荐指数
1
解决办法
1411
查看次数

ArrayList赋值

我在我的main方法中编写了以下代码:

    ArrayList<String> finalPhrases = new ArrayList<String>();
    switch (response){
    //TODO: FIX
        case 2: finalPhrases = modeOne(inputScanner, patternScanner);
        case 1: finalPhrases = modeTwo(inputScanner, patternScanner);
        case 0: finalPhrases = modeThree(inputScanner, patternScanner);
    }   

    System.out.println(finalPhrases);
Run Code Online (Sandbox Code Playgroud)

其中(response)是一个变量,它将是2,1,0和modeOne,Two,Three是我写的返回ArrayList的方法.我遇到了一个问题,当我尝试打印finalPhrases时,它表明ArrayList是空的,我认为这可能与内存引用有关.

我知道方法运行正常,因为我跑的时候

System.out.println(modeOne(inputScanner, patternScanner)) 
Run Code Online (Sandbox Code Playgroud)

它打印出正确的arraylist,但是一旦我分配它,它就会失败.谁能告诉我如何解决这个问题?

java arraylist

0
推荐指数
1
解决办法
476
查看次数

在java中创建一个包含10个唯一数字的数组

这是我第一次问这个问题.

我想用0到9的10个唯一的int数字制作ArrayList.我接下来的步骤:

  1. 创建空Arraylist
  2. 添加第一个随机数,以便稍后检查重复
  3. 接下来我创建新的随机int值,检查我是否已经在ArrayList中使用了这个值.如果我有 - 我尝试另一个号码,如果我没有 - 我添加这个号码.
  4. 如果我得到10个数字,我会停止循环

我的代码:

public static void main(String[] args) {

    Random rd = new Random();
    ArrayList<Integer> list = new ArrayList<Integer>();

    int q = rd.nextInt(10);
    list.add(q);

    while (true) {
        int a = rd.nextInt(10);
        for (int b=0;b<list.size();b++){
            if (a == list.get(b)) break;
            else list.add(a);
        }
        if (list.size() == 10) break;
    }
    System.out.println(list);
}
Run Code Online (Sandbox Code Playgroud)

但我在控制台看到的只是无休止的过程.

问题是 - 是否有另一种方法可以使ArrayList具有10个唯一数字(0到9)?

java arraylist

0
推荐指数
1
解决办法
1347
查看次数

从适配器android返回更改的arrayList

有gridview,其中设置适配器名为ImageAdapter,参数为arraylist.在适配器内部有onclicklistener,在此期间从arraylist中删除一个项目,然后当我ImageAdapter.notifyDataSetChanged在gridview项目中使用此行时将被删除.现在我需要在我的活动中更改arrayList,我怎么能得到它.

这是我的代码:

public class ImageAdapter extends BaseAdapter {
Context context;
ArrayList<String> listCheck = new ArrayList<String>(); 
ImageAdapter adapter = this;

    public ImageAdapter(Context context, ArrayList<String> list) { 
    this.context = context; 
    listCheck = list; 
}    
    @Override
public int getCount() {
    return listCheck.size();
}

    @Override
public Object getItem(int arg0) {
    return null; 
}

    @Override
public long getItemId(int arg0) {
    return 0;
}

    @Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder121 holder;
    if (convertView == null) {
        convertView = …
Run Code Online (Sandbox Code Playgroud)

java android gridview arraylist

0
推荐指数
1
解决办法
3035
查看次数

在保持多态属性的同时创建一个超类数组

如果我在运行时填充了一系列Birds,那么如何访问特定于子类的成员方法?

class Bird
{
  public Bird() {}
  public void fly(int x) {
     System.out.println("Flew "+x+" meters");
  }
}
class DumbBird extends Bird
{
   public DumbBird() {super();}
   public void fly(int x) {
     x-=5; //we're dumb
     System.out.println("Flew "+x+" meters");
   }
   public void sing() {
      System.out.println("La la la!");
   }
}
public static void main(String[] args)
{
   Bird[] cage = new Bird[10];
   cage[0] = new Bird();
   cage[1] = new Dumbbird();
   cage[2] = new Sleepybird();
   //.... more bird types

   cage[1].sing(); //is inaccessable because it is of …
Run Code Online (Sandbox Code Playgroud)

java arrays polymorphism arraylist

0
推荐指数
1
解决办法
44
查看次数

删除一个列表中的所有元素并添加到另一个列表 - Java

所以我试图从玩家的'Rack'(一个ArrayList)中删除所有牌,并将其逐个放入丢弃堆(Stack)中.到目前为止,我有以下代码,我已经意识到一旦它达到5就会停止.(注意:每个玩家机架有10张卡).

        int rackSize = player.getPlayerRack().getRack().size(); // rackSize = 10
        for (int i = 0; i < rackSize; i++) {
            getDeck().getDiscardPile().add(player.getPlayerRack().getRack().remove(i));
        }
Run Code Online (Sandbox Code Playgroud)

我的问题是如何删除玩家'Rack'中的所有项目,使rackSize = 0,并将所有项目添加到弃牌堆中?

如果这令人困惑,非常抱歉.你可以通过说a中有10个整数来概括它ArrayList<Integer> hand,所以:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].如何从此列表中删除所有项目并将其添加到Stack<Integer> discardPile?

java stack arraylist

0
推荐指数
1
解决办法
989
查看次数

使用While循环添加到数组列表

我有一个包含书籍的数组列表.我想要做的(出于各种原因)是使用While循环继续将书籍添加到我的数组列表,直到我告诉程序停止.为了让它停止,我使用扫描仪询问人类用户是否应该添加另一本书.如果用户说是,则该方法将重复,否则该方法将停止.

不幸的是,我被困在两个方面(现在).首先,我不知道如何将决定插入到方法中,我试过:

while(decision.equals("Yes"){addBooks(anyTitle,anyAuthor);},
Run Code Online (Sandbox Code Playgroud)

然而,这似乎是错误的,因为它进入一个循环(虽然我想),但它会导致2个问题:A)即使我输入其他东西,该方法也不会结束(除非我使用另一个扫描仪并包含一个中断),和B)相同的输入字段一遍又一遍地插入到数组列表中,而没有给我输入新字符串的机会.

/**
 * Method to add a single book
 */
public void setOneBook(String anyTitle, String anyAuthor){
    bookList.add (new Book(anyTitle,anyAuthor));
}

/**
 * Method to add books to collection until the command to stop is given.
 */
public void addBooks(String anyTitle, String anyAuthor){
    bookList.add (new Book(anyTitle,anyAuthor));
    System.out.println("Would you like to add another book?");
    String decision = keybd.next();
}
Run Code Online (Sandbox Code Playgroud)

我此刻感到非常愚蠢,所以请对自己保持嗤之以鼻的评论.

虽然这样做了,但我会搞乱我的代码,所以如果有任何变化(无论好坏),我会更新我的帖子.

java arraylist while-loop

0
推荐指数
1
解决办法
2081
查看次数

ArrayList - 可以读出元素名称吗?

Heyho大家!

我的项目有点大,所以我决定将它缩短一点,只显示我目前的问题代码.首先,在控制台上进行im编程.从扫描仪中读取六个字符串,然后将它们保存在一个变量中,我正在进行有效性检查(长度,特殊标志等).所以我决定用额外的方法check_newCustomer()来做这个.我使用ArrayList作为一个值返回更多.所以现在我需要在main()函数中捕获输入或者在数据库中编写新Customer的任何其他方法.问题是现在我不知道如何在其他方法中引用userID,firstname,secondname .... 我只能用索引来引用.但是当我可以使用变量名称来引用它时,我更喜欢它.所以在其他方法上处理字符串要容易得多.可能?

public static void main(String[] args) {
    ArrayList<String> newCustomer = new ArrayList<String>(); 
    check_newCustomer (newCustomer);
}


public static void check_newCustomer(ArrayList<String> newCustomer) throws IOException {
    String userID = null;
    String firstname = null;
    String secondname = null;
    String street = null;
    String zipcode = null;
    String city = null;

    // validity check before I fill the input fields
    ...

    // fill arraylist
    newCustomer.add(userID);
    newCustomer.add(firstname);
    newCustomer.add(secondname);
    newCustomer.add(street);
    newCustomer.add(zipcode);
    newCustomer.add(village);
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

java arraylist

0
推荐指数
1
解决办法
79
查看次数

ArrayList不存储值

我是ArrayLists概念的新手.我正在使用它们,因为我想要一个动态数组,其数量不限于它可以按顺序保存多少个值.但我使用的方法不是正确存储值.我只保留一个值为1.我的代码如下:

public void Rand(){
Random rand = new Random();
int Val = rand.nextInt(5); 
ArrayList<Integer> ValList = new ArrayList<Integer>();
ValList.add(Val);
Log.d("LOOK", Integer.toString(ValList));   
Log.i("VALUE LIST ", ValList.size()+" <<<<LIST HERE"); }
Run Code Online (Sandbox Code Playgroud)

此过程在oncreate中调用,然后返回到循环中.理想情况下,每次运行此类时,新的随机数将按顺序添加到数组中.

这是logcat的相关信息:

03-05 19:15:15.020: D/LOOK(21325): 3
03-05 19:15:15.020: I/VALUE LIST(20883): 1 <<<<LIST HERE
03-05 19:15:15.040: D/LOOK(21325): 5
03-05 19:15:15.040: I/VALUE LIST(20883): 1 <<<<LIST HERE
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,即使随机值不是1,它也只存储值1.我使用了错误类型的数组还是错误地实现了它?

我希望看到的输出是这样的:

03-05 19:15:15.020: D/LOOK(21325): 3
03-05 19:15:15.020: I/VALUE LIST(20883): 3 <<<<LIST HERE
03-05 19:15:15.040: D/LOOK(21325): 5
03-05 19:15:15.040: I/VALUE LIST(20883): 3, 5 <<<<LIST HERE
Run Code Online (Sandbox Code Playgroud)

java random android arraylist logcat

0
推荐指数
1
解决办法
1165
查看次数

是在ArrayList类中重写了toString()

class Employee
{
    String name ;
    // Constructor 
    Employee(String name) {
        this.name = name;
    }
    // override toString method in Employee class
    @Override
    public String toString() {
        return name;
    }
}

public class TestArraylistIterator {

    public static void main(String[] args) {
        Employee obj1 = new Employee("Java");
        Employee obj2 = new Employee("Microsoft");

        TestArraylistIterator obj3 = new TestArraylistIterator();

        List ls = new ArrayList();
        ls.add(obj1);
        ls.add(obj2);               
        System.out.println("List object :: " + ls); 
        System.out.println("TestArraylistIterator :: " + obj3);     
    }
}

output : 
List object …
Run Code Online (Sandbox Code Playgroud)

java arraylist tostring

0
推荐指数
1
解决办法
69
查看次数