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

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

问题描述

我需要知道我是否将数据存储在ArrayList中,并且需要获取存储在其中的值。

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

我想得到每个String元素,我该怎么做?

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

我试过以下代码:

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


推荐答案

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

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?"

请注意以下事项:


  • 通用列表< String> 使用ArrayList< String> 代替raw ArrayList type。

  • 变量名以小写字母开头

  • list 声明为 List< String> ,即接口类型而不是实现类型 ArrayList< String>

  • 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>.

  • Java Collections Framework教程

  • class ArrayList< E>实施清单< E>

  • interface List< E>


    • E get(int index)


      • 返回此列表中指定位置的元素。

      • Java Collections Framework tutorial
      • class ArrayList<E> implements List<E>
      • interface List<E>
        • E get(int index)
          • Returns the element at the specified position in this list.

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


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


          如果您使用原始类型,则会失去仿制药的所有安全性和表现力。



          • Effective Java 2nd Edition:Item 52:Refer to对象的接口

          • 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.



          变量:除了变量之外,所有实例,类和类常量都是混合大小写的小写的第一个字母

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

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