简体   繁体   English

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

[英]ArrayList - possible to read out the element name?

Heyho everyone! 大家好!

My project is a bit bigger so I decided to short it a bit and showing only the problem code, which i have currently. 我的项目更大,所以我决定将其简短一点,只显示我当前拥有的问题代码。 On the first, im programming on a console. 首先,在控制台上进行即时编程。 Reading six strings from a Scanner, before I save them in a variable i'm doing a validity check (length, special signs, etc...). 从扫描仪读取六个字符串,然后将它们保存到变量中,然后进行有效性检查(长度,特殊符号等)。 So I decided to make this in an extra method check_newCustomer(). 因此,我决定使用额外的方法check_newCustomer()来完成此操作。 I used an ArrayList to return more as one value. 我使用ArrayList作为一个值返回更多值。 So now is the point that I need the captured inputs in the main() function or any other method which writes the new Customer in the database. 现在,我需要在main()函数或任何其他将新客户写入数据库的方法中捕获输入。 Problem is now i don't know how I can reference to userID, firstname, secondname... in other method. 现在的问题是我不知道如何在其他方法中引用userID,firstname,secondname...。 I just can refer with an index. 我可以参考一个索引。 But I would prefer it when i can use the variable names to refer to it. 但是当我可以使用变量名来引用它时,我会更喜欢它。 So its much easier on the other methods to handle with strings. 因此,使用其他方法处理字符串要容易得多。 Possible? 可能?

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);
}

Thanks! 谢谢!

No, the value in the ArrayList is just a reference. 不, ArrayList的值只是一个参考。 The fact that you originally referred to it using a different variable is irrelevant. 您最初使用其他变量引用它的事实是无关紧要的。

You could use a Map<String, String> instead... but it would be much cleaner to have a Customer class which had fields for the various pieces of information. 可以使用Map<String, String>代替...但是拥有一个Customer类,其中包含用于各种信息的字段会清洁。 If you want multiple customers, you can then have a List<Customer> . 如果需要多个客户,则可以有一个List<Customer>

One needs to make a class Customer, just a group of fields. 一个人需要创建一个类Customer,只需一组字段即可。 Instead of a list of Strings. 而不是字符串列表。

public class Customer {
    String userID;
    String firstname;
    String secondname;
    String street;
    String zipcode;
    String city;
}

And in the code: 并在代码中:

Customer newCustomer = new Customer();
newCustomer.userID = ....
System.out.println(newCustomer.userID);

When you pass check_newCustomer (newCustomer); 当您通过check_newCustomer (newCustomer); you are only passing a copy of the array list. 您只传递数组列表的副本。 In this case, the original array list newcustomer is left intact while a new copy whose scope is within the method check_newCustomer is where all the strings are stored. 在这种情况下,原始数组列表newcustomer保持原样,而范围在方法check_newCustomer内的新副本将存储所有字符串。 You can either create a new class for Customers or you can create a class and have your array list as a class variable and check_newCustomer as a class method. 您可以为客户创建一个新类,也可以创建一个类并将数组列表作为类变量,将check_newCustomer作为类方法。 In the latter case, it is much simple. 在后一种情况下,这很简单。

    class customer
        ArrayList<String> newCustomer;
        public static void main(String[] args) {

                newCustomer = new ArrayList<String>(); 
                check_newCustomer ();
            }


        public static void check_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);
                }
}

This must work. 这必须工作。

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

相关问题 是否有可能找出一个值在数组列表中是否存在两次? - Is it possible to find out if a value exists twice in an arraylist? 使用ObjectInputStream从文件中读取许多ArrayList对象吗? - Read many ArrayList Objects out of File with ObjectInputStream? 打印出 ArrayList 的所有可能组合<ArrayList<String> &gt; 递归地 - Print out all possible combinations of an ArrayList<ArrayList<String>> recursively 从组织好的ArrayList中挑选一个随机元素 - Picking out a random element from an organised ArrayList 是否可以在for()周期内擦除ArrayList元素? - Is possible to erase an ArrayList element inside a for( ) cycle? 无法打印出ArrayList(代码中可能发生逻辑错误) - Unable to print out ArrayList (Possible Logic Error in Code) 是否可以将int添加到String ArrayList中而无需在Java中将int转换为String - Is it possible to add int into the String ArrayList with out Converting int to String in java 如何从arraylist中读取没有符号的多个整数? - How to read multiple integers without symbols out of arraylist? 尝试从文件读取到ArrayList时,索引超出范围异常 - Index Out Of Bounds Exception when trying to read from file into ArrayList 如何将读取对象添加到ArrayList并打印出用户请求的信息 - How to add in a read object to an ArrayList and print out user requested info
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM