简体   繁体   English

如何打印阵列列表,以及如何向阵列列表添加错误检查?

[英]How can I print out an arraylist and also How can I add errors checks to the arraylist?

I have been trying to solve this problem for ages and with no luck I didn't progress. 我多年来一直试图解决这个问题,但没有运气,我没有进步。 Could someone please help me out. 有人可以帮我吗。 I have created an arrayList, made an getter class, have made a method. 我创建了一个arrayList,创建了一个getter类,并创建了一个方法。 I can add stuff to the array list as well but when I print the arrayList out it prints out some random text. 我也可以向数组列表中添加内容,但是当我打印出arrayList时,它会打印出一些随机文本。

below is the arrayList I created. 下面是我创建的arrayList。

public static ArrayList<getArrayList> arrayList = new ArrayList<getArrayList>();

here is my method; 这是我的方法;

private static void addToArrayList(String a, double no1, int no2, int no3) {

        try {
            arrayList.add(new getArrayList(a, no1, no2, no3));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

here is my getter class 这是我的吸气类

public class getArrayList {
    private String name;
    private double seconds;
    private int speed1;
    private int speed2;


    public String getName() {
        return name;
    }



    public double getSeconds() {
        return seconds;
    }



    public int getSpeed1() {
        return speed1;
    }



    public int getSpeed2() {
        return Speed2;
    }



    public StoreCommands(String storeName, double storeSeconds, int storeSpeed1, int storeSpeed2) throws Exception{
        name =  storeName;
        seconds = storeSeconds;
        speed1 = storeSpeed1;
        speed2 = storeSpeed2;

        }

    }

to add stuff on this list I use the method I created 在此列表上添加内容我使用我创建的方法

addToArrayList(String a, double no1, int no2, int no3) filled in with my values

and to receive stuff from the arraylist I use this 并从arraylist接收东西,我用这个

  for(getArrayList s : arrayList) {
System.out.println(arrayList + "\n")
        ;

and it prints out if i use System.out.println(arrayList), depending on how much I add to the arraylist. 如果我使用System.out.println(arrayList),它会打印出来,具体取决于我添加到arraylist的数量。

[StoreCommands@49a21b63]

Besides that could someone tell me how I can set a size of the arrayList so if anything more that is being added it won't add and give an error. 除此之外,有人可以告诉我如何设置arrayList的大小,这样,如果要添加的其他内容将不会添加并给出错误。 Also I need to perform certain error checks once the items are in the arrayList *1st If the an item is added to the list and the user tries to add the same one again straight after I want to display an error.. (the user can add the same stuff but not directly after the one they just added, they will need to add something else first) *2nd if say user wants to add apples to the list, I want to limit that to only 2 time in the whole list, more than that will not be added and will display and error. 此外,一旦项目位于arrayList * 1st中,我还需要执行某些错误检查。如果将一项添加到列表中,并且用户在我想显示错误之后尝试再次添加相同的项目。(用户可以添加相同的内容,但不能直接添加到刚添加的内容之后,则需要先添加其他内容。)* 2如果用户要向列表中添加苹果,我想将其限制为整个列表中只有2次,超过此数量的内容将不会添加,并且会显示并显示错误。

Could someone help me out please, I will really appreciate it. 有人可以帮帮我,我将非常感激。

Thanks. 谢谢。

Try this - 尝试这个 -

for(getArrayList s : arrayList)
     System.out.println(s + "\n");//This print the tostring of getArrayList class

Again override the toString method of getArrayList class, to print actual field value of the object. 再次重写getArrayList类的toString方法,以打印对象的实际字段值。 Example - 范例-

public class getArrayList {
     public String toString(){
          return name +"||" +seconds+"||"+ speed1+"||"+speed2;
     }
}

Note : Follow java nomenclature standard, first later of you class name would be capital And also give a relevent name to the Class. 注意:遵循Java命名标准,您的类名的后半部分将是大写字母,并为该类提供一个相关的名称。

You don't have to loop in order to print an array, just do: 您不必循环即可打印数组,只需执行以下操作:

System.out.println(Arrays.toString(arrayList.toArray()));

ArrayList doesn't have a mechanism to limit the size, if you want to do it - you'll have to implement it. ArrayList没有限制大小的机制,如果要这样做,则必须实现它。

Example: 例:

import java.util.ArrayList;    
/**
 * User: alfasin
 * Date: 2/5/14
 */
public class RestrictedSizeArrayList<E> extends ArrayList<E> {

    private static final int limit = 6;//example

    @Override
    public boolean add(E e){
        if(this.size() > 5){
            throw new IndexOutOfBoundsException("Can't add more than 5 elements to the ArrayList");
        }
        boolean result = super.add(e);
        return result;
    }
}

Overriding toString method will help you to print actual data. 覆盖toString方法将帮助您打印实际数据。 Override it in your class getArrayList . 在您的class getArrayList重写它。 One more thing is class name should start with capital letter. 还有一件事是类名应以大写字母开头。

public String toString()
{
    return "Name  :  "+name+"  Seconds  : "+seconds+"  Speed1  :  "+speed1+"  Speed2  :  "+speed2;
}

and use it like 并像这样使用

for(getArrayList s : arrayList)
   System.out.println(s.toString);

You can limit the size by adding check to 您可以通过添加支票来限制大小

private static void addToArrayList(String a, double no1, int no2, int no3) {

        try 
        {
           if(arrayList.size < 10) //any size you want
             arrayList.add(new getArrayList(a, no1, no2, no3));
           else
             System.out.println("ArrayList is full");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM