Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java编程语言的数组列表_Java_Arrays_List_Data Structures - Fatal编程技术网

使用java编程语言的数组列表

使用java编程语言的数组列表,java,arrays,list,data-structures,Java,Arrays,List,Data Structures,如何使用java编程语言创建数组列表,在每个记录中包含多个信息 比如 您要做的是面向对象编程和类的典型案例。例如: import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public enum Gender { male, female } public static class Person {

如何使用java编程语言创建数组列表,在每个记录中包含多个信息 比如


您要做的是面向对象编程和类的典型案例。例如:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main
{

    public enum Gender
    {
        male, female
    }

    public static class Person
    {
        public String name;
        public Integer age;
        public Gender gender;

        public Person(String name, Integer age, Gender gender)
        {
            this.name = name;
            this.age = age;
            this.gender = gender;
        }

        public String toString()
        {
            return name + "("+ age + " " + gender + ")";
        }
    }

    public static void main(String[] args)
    {
        // example with array
        Person stefan = new Person("Stefan", 45, Gender.male);
        Person maria = new Person("Maria", 43, Gender.female);
        Person[] array={stefan, maria};
        System.out.println("array=" + Arrays.asList(array));

        // example with ArrayList
        List<Person> arrayList = new ArrayList<Person>();
        arrayList.add(new Person("Robert", 40, Gender.male));
        arrayList.add(new Person("Lisa", 41, Gender.female));
        System.out.println("arrayList="+arrayList);

        // access individual attributes
        Person first = arrayList.get(0);
        System.out.println("first name="   + first.name);
        System.out.println("first age="    + first.age);
        System.out.println("first gender=" + first.gender);
    }
}

---请阅读:---您不需要使用
ArrayList
。如果是所有字符串,则可以使用
Map
或实际的类。或者等待记录在Java中实际成为一个对象:)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main
{

    public enum Gender
    {
        male, female
    }

    public static class Person
    {
        public String name;
        public Integer age;
        public Gender gender;

        public Person(String name, Integer age, Gender gender)
        {
            this.name = name;
            this.age = age;
            this.gender = gender;
        }

        public String toString()
        {
            return name + "("+ age + " " + gender + ")";
        }
    }

    public static void main(String[] args)
    {
        // example with array
        Person stefan = new Person("Stefan", 45, Gender.male);
        Person maria = new Person("Maria", 43, Gender.female);
        Person[] array={stefan, maria};
        System.out.println("array=" + Arrays.asList(array));

        // example with ArrayList
        List<Person> arrayList = new ArrayList<Person>();
        arrayList.add(new Person("Robert", 40, Gender.male));
        arrayList.add(new Person("Lisa", 41, Gender.female));
        System.out.println("arrayList="+arrayList);

        // access individual attributes
        Person first = arrayList.get(0);
        System.out.println("first name="   + first.name);
        System.out.println("first age="    + first.age);
        System.out.println("first gender=" + first.gender);
    }
}
array=[Stefan(45 male), Maria(43 female)]
arrayList=[Robert(40 male), Lisa(41 female)]
first name=Robert
first age=40
first gender=male