如何在 Java 中使用数组列表? [英] How to use an array list in Java?

查看:21
本文介绍了如何在 Java 中使用数组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道我是否将我的数据存储在一个 ArrayList 中,我需要获取我存储在其中的值.

例如:如果我有这样的数组列表

 ArrayList A = new ArrayList();A = {"Soad", "mahran"};

我想获取每个String元素,我该怎么做?

我尝试通过以下代码来做到这一点:

包数组列表;导入 java.util.ArrayList;公共课主要{公共静态无效主(字符串 [] args){ArrayList S = new ArrayList();字符串 A = "soad ";S.add(A);S.add("A");String F = S.toString();System.out.println(F);String [] W = F.split(",");for(int i=0 ; i

解决方案

以下代码段给出了一个示例,展示了如何从指定索引处的 List 中获取元素,以及如何使用高级 for-each 循环遍历所有元素:

 import java.util.*;//...列表<字符串>list = new ArrayList();list.add("你好!");list.add("你好吗?");System.out.println(list.get(0));//打印你好!"for (String s : list) {System.out.println(s);}//打印你好!",你好吗?"

注意以下几点:

  • 使用通用ListArrayList 类型而不是原始ArrayList 类型.
  • 变量名以小写开头
  • list 声明为List,即接口类型而不是实现类型ArrayList.
<小时>

参考文献

API:

不要使用原始类型

  • JLS 4.8 原始类型

    <块引用>

    仅允许使用原始类型作为对遗留代码兼容性的让步.强烈反对在将泛型引入 Java 编程语言之后编写的代码中使用原始类型.Java 编程语言的未来版本可能会禁止使用原始类型.

  • Effective Java 2nd Edition:条款 23:不要在新代码中使用原始类型

    <块引用>

    如果使用原始类型,则会失去泛型的所有安全性和表达性优势.

在类型声明中优先使用接口而不是实现类

  • Effective Java 2nd Edition:第 52 条:通过接口引用对象<块引用>

    [...] 你应该倾向于使用接口而不是类来引用对象.如果存在合适的接口类型,那么参数、返回值、变量和字段都应该使用接口类型声明.

命名约定

<块引用>

变量:除变量外,所有实例、类和类常量都是大小写混合的,首字母小写.

I need to know if I store my data in an ArrayList and I need to get the value that I've stored in it.

For example : if I have an array list like this

      ArrayList A = new ArrayList();
      A = {"Soad", "mahran"};

and I want to get each String element, how can I do it?

I've tried to do it by the following code:

package arraylist;

import java.util.ArrayList;

public class Main {

        public static void main(String[] args) {
        ArrayList S = new ArrayList();

        String A = "soad ";
        S.add(A);
        S.add("A");
        String F = S.toString();
        System.out.println(F);
        String [] W = F.split(",");
        for(int i=0 ; i<W.length ; i++) {
           System.out.println(W[i]);
        }
    }
}

解决方案

The following snippet gives an example that shows how to get an element from a List at a specified index, and also how to use the advanced for-each loop to iterate through all elements:

    import java.util.*;

    //...

    List<String> list = new ArrayList<String>();
    list.add("Hello!");
    list.add("How are you?");

    System.out.println(list.get(0)); // prints "Hello!"

    for (String s : list) {
        System.out.println(s);
    } // prints "Hello!", "How are you?"

Note the following:

  • Generic List<String> and ArrayList<String> types are used instead of raw ArrayList type.
  • Variable names starts with lowercase
  • list is declared as List<String>, i.e. the interface type instead of implementation type ArrayList<String>.

References

API:

Don't use raw types

  • JLS 4.8 Raw Types

    The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

  • Effective Java 2nd Edition: Item 23: Don't use raw types in new code

    If you use raw types, you lose all the safety and expressiveness benefits of generics.

Prefer interfaces to implementation classes in type declarations

  • Effective Java 2nd Edition: Item 52: Refer to objects by their interfaces

    [...] you should favor the use of interfaces rather than classes to refer to objects. If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types.

Naming conventions

Variables: Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.

这篇关于如何在 Java 中使用数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆