ArrayList的使用

ArrayList

一、ArrayList创建变量的步骤

1.导入包

import java.util.*;

2.创建引用类型的变量

数据类型<集合存储的数据类型> 变量名 = new 数据类型<集合存储的数据类型>();

集合存储的数据类型:要将数据存储到集合的容器中。创建集合引用变量的时候,必须要指定好存储的类型是什么。

3.变量名.方法即可调用

注意尖括号中的类型必须要写。

注意:ArrayList存储的是引用类型,那么8个基本的类型对应8个引用类型。

如下表所示:

表1基本数据类型&引用数据类型参照表 

基本数据类型

对应的引用数据类型
表示形式

byte

Byte

short

Short

int

integer

long

Long

float

Float

double

Double

char

Character

boolean

Boolean

二、ArrayList常见的方法
array.add(object);//添加一个元素
array.get(index);//取出集合中的元素,在get方法的参数中,写入索引。
array.size();//返回集合的长度,也就是存储元素的个数。
array.set(object);//设置一个元素
array.remove();//移除一个元素
这里需要注意的是remove()方法,remove()会改变索引,在某些循环中会漏掉遍历的元素,在下题中可以得到体现:
 
题目:

分析以下需求并实现
1.创建一个ArrayList集合,用于存储一些字符串:"abc","def","def",
"ghi","def","hij","jkol"
2.遍历集合,统计集合中"def"字符串一共出现了多少个
3.将集合中的所有"def"字符串删除。打印删除后的集合元素

 1 package pers.ccsoft.lucifer;
 2 
 3 import java.util.ArrayList;
 4 
 5 /*
 6 * 第四题:分析以下需求并实现
 7     1.创建一个ArrayList集合,用于存储一些字符串:"abc","def","def","ghi","def","hij","jkol"
 8     2.遍历集合,统计集合中"def"字符串一共出现了多少个
 9     3.将集合中的所有"def"字符串删除。打印删除后的集合元素
10     创建时间:2018年7月22日20:42:24
11     创建者:ccsoftlucifer
12 * */
13 public class StringArrayListText {
14     public static void main(String[] args){
15         ArrayList<String> obj = new ArrayList<String>();
16         obj.add("abc");
17         obj.add("def");
18         obj.add("def");
19         obj.add("ghi");
20         obj.add("def");
21         obj.add("hij");
22         obj.add("jkol");
23         System.out.println("当前的集合中的元素为:");
24         for(int i=0;i<obj.size();i++)
25         {
26            System.out.print(obj.get(i)+" ");
27         }
28         System.out.println();
29 
30         int count=0;
31 
32         for(int i=0;i<obj.size();i++)
33         {
34             if(obj.get(i).equals("def")){
35                 count++;
36             }
37         }
38         System.out.println("def一共出现了"+count+"次");
39 
40         System.out.println("开始删除集合中的def字符串,请稍候...");
41         for(int i=0;i<obj.size();i++)
42         {
43             if(obj.get(i).equals("def")){
44                 obj.remove(i);
45                 i-=1;//remove方法 会改变ArrayList的索引,需要重新扫描该位置的元素,以免漏掉
46             }
47         }
48         System.out.println("当前的集合中的元素为:");
49         for(int i=0;i<obj.size();i++)
50         {
51             System.out.print(obj.get(i)+" ");
52         }
53     }
54 }

程序执行的结果为:

如果删掉第45行,会出现一个问题:

会发现有一个元素并没有删除,这是因为在对 abc def def ……元素进行删除的时候,第二次循环找到了def, 使用了remove方法将这个元素删除,与此同时,

def右侧的所有元素左移,第二个def移动到了第一个def的位置,当索引继续遍历的时候,相当于跳过了第二个def,使得结果有误。所以要在这个地方使索引

回退一个,然后继续遍历,这样结果就复合预期。


题目:
1.玩法说明:
*双色球投注区分为红球号码区和蓝球号码区,红球号码范围为01~33,蓝球号码范围为01~16。
*双色球每期从33个红球中开出6个号码,从16个蓝球中开出1个号码作为中奖号码,
*双色球玩法即是竞猜开奖号码的6个红球号码和1个蓝球号码,顺序不限 。
 1 public class Topic3 {
 2     public static void main(String[] args) {
 3        String stringArr[] = new String[7];
 4         //先确定蓝球位置 标记为flag.
 5         int flag = 0;
 6         Random ra = new Random();
 7         int blueBallIndex = ra.nextInt(7);
 8         stringArr[blueBallIndex]=method2();
 9         for (int i = 0; i < stringArr.length; i++) {
10             if(i==blueBallIndex)
11                 continue;
12             stringArr[i]=method1();
13         }
14         for (int i = 0; i < stringArr.length; i++) {
15             System.out.print(stringArr[i]+" ");
16         }
17     }
18    //method1 返回红球字符串
19     public static String method1(){
20         ArrayList<String> arrayList = new ArrayList<>();
21         for (int i = 0; i < 33; i++) {
22             Integer temp = (i+1);
23             arrayList.add(temp.toString());
24         }
25         Random ra = new Random();
26         int randomValue = ra.nextInt(33);
27         return "红球"+arrayList.get(randomValue);
28     }
29     //method2 返回蓝球字符串
30     public static String method2(){
31         ArrayList<String> arrayList = new ArrayList<>();
32         for (int i = 0; i < 6; i++) {
33             Integer temp = (i+1);
34             arrayList.add(temp.toString());
35         }
36         Random ra = new Random();
37         int randomValue = ra.nextInt(6);
38         return "蓝球"+arrayList.get(randomValue);
39     }
40 
41 }
 
 
三、ArrayList初步使用
1.实现学生数据的录入功能
* 2.
实现学生数据的查询功能
* 3.
实现学生数据的遍历功能
* 4.
实现学生数据的删除功能
* 5.
实现学生数据的更改功能
  1 import javafx.application.Platform;
  2 
  3 import java.lang.reflect.Array;
  4 import  java.util.*;
  5 import java.awt.*;
  6 /*
  7 * 程序功能:学生信息录入程序
  8 * 1.实现学生数据的录入功能
  9 * 2.实现学生数据的查询功能
 10 * 3.实现学生数据的遍历功能
 11 * 4.实现学生数据的删除功能
 12 * 5.实现学生数据的更改功能
 13 * */
 14 public class ArrayTest {
 15    static ArrayList<Student> stu = new ArrayList<Student>();
 16 
 17    public static void anyKeyContinune()
 18    {
 19        System.out.println("请按任意键继续");
 20        Scanner input = new Scanner(System.in);
 21        String str = input.next();
 22    }
 23     /*menu():该静态方法用来显示目录字符串信息*/
 24     public static void menu(){
 25         System.out.println("学生信息录入程序
	1.添加学生信息
	2.查询学生信息
	" +
 26                 "3.查询所有学生信息
	4.删除指定学生信息
	5.更改指定学生信息
	0.退出

请选择:");
 27     }
 28     /*添加学生信息*/
 29     public static void addStu()
 30     {
 31        /*   1.学生姓名name
 32             * 2.学生学号sno
 33             * 3.学生成绩score*/
 34         Student no1 = new Student();
 35         Scanner sc = new Scanner(System.in);
 36         System.out.println("请添加学生的姓名");
 37         no1.name=sc.nextLine();//用nextLine()方法获取字符串
 38         System.out.println("请添加学生的学号");
 39         no1.sno=sc.nextLong();
 40         System.out.println("请添加学生的成绩");
 41         no1.score =sc.nextInt();
 42         stu.add(no1);
 43         //System.out.println(stu.get(0).name +" "+ stu.get(0).sno + " "+stu.get(0).score);
 44 
 45     }
 46     /*查询学生信息 默认姓名查询*/
 47     public static void searchName()
 48     {
 49         System.out.println("请输入想要查询的姓名:");
 50         Scanner sc = new Scanner(System.in);
 51         int flag =0;
 52         String searName = sc.nextLine();
 53         for(int i=0;i<stu.size();i++)
 54         {
 55             if(stu.get(i).name.equals(searName))
 56             {
 57                 System.out.println("学生的信息是:
name	  sno		   score
"+stu.get(i).name +" "+ stu.get(i).sno + " "+stu.get(i).score);
 58                 flag=1;
 59             }
 60         }
 61         if(flag==0)
 62             System.out.println("不好意思,查无此人!");
 63         anyKeyContinune();
 64     }
 65 
 66     /*3.查询所有学生信息*/
 67     public static void View()
 68     {
 69         System.out.println("所有的学生信息如下
name	  sno		  score");
 70         for(int i=0;i<stu.size();i++)
 71             System.out.println(stu.get(i).name +" "+ stu.get(i).sno + " "+stu.get(i).score);
 72         anyKeyContinune();
 73     }
 74     /*4.删除指定学生信息*/
 75     public static void deleteStu()
 76     {
 77         System.out.println("请输入想要查询的姓名:");
 78         Scanner sc = new Scanner(System.in);
 79         int flag =0;
 80         String searName = sc.nextLine();
 81         int i;
 82         for( i=0;i<stu.size();i++)
 83         {
 84             if(stu.get(i).name.equals(searName))
 85                {
 86                    System.out.println("学生的信息是:
name	  sno		   score
"+stu.get(i).name +" "+ stu.get(i).sno + " "+stu.get(i).score);
 87                     flag =1;
 88                }
 89         }
 90         if(flag==0)
 91             System.out.println("不好意思,查无此人!");
 92         else{
 93 
 94             System.out.println("是否想要删除? yes or no ?");
 95             String conform = sc.nextLine();
 96             if(conform.equals("yes"))
 97             {
 98                 System.out.println("正在删除,请稍后!");
 99                 stu.remove(i-1);
100                 System.out.println("删除成功!");
101             }
102             else
103             {
104                 System.out.println("输入错误 或者 不想删除!请想清楚后再尝试!");
105             }
106         }
107 
108         anyKeyContinune();
109     }
110     /*5.更改指定学生信息*/
111     public static void change()
112     {
113         System.out.println("请输入想要修改的学生信息,以姓名查找:");
114         int flag =0;
115         Scanner sc = new Scanner(System.in);
116         String searName = sc.nextLine();
117         int i;
118         for( i=0;i<stu.size();i++)
119         {
120             if(stu.get(i).name.equals(searName))
121             {
122                 System.out.println("学生的信息是:
name	  sno		   score
"+stu.get(i).name +" "+ stu.get(i).sno + " "+stu.get(i).score);
123                 flag=1;
124             }
125         }
126         if(flag==0)
127             System.out.println("不好意思,查无此人!");
128         else {
129            // Scanner sc = new Scanner(System.in);
130             Student p1 = new Student();
131 
132             System.out.println("请重新输入学生的基本信息
请重新输入学生的姓名");
133             p1.name=sc.nextLine();//用nextLine()方法获取字符串
134             System.out.println("请重新输入的学号");
135             p1.sno=sc.nextLong();
136             System.out.println("请重新输入的成绩");
137             p1.score=sc.nextInt();
138             stu.set(i-1,p1);
139             System.out.print("修改成功!");
140         }
141 
142 
143         }
144     public static void main(String[] args) throws   Exception{
145         for (;;)
146         {
147             //显示菜单
148             menu();
149             //选择选项
150             int choice;
151             Scanner sc = new Scanner(System.in);
152             //ArrayList<Student> stu = new ArrayList<Student>();
153             choice = sc.nextInt();
154             if (choice==1){
155                 addStu();
156             }
157             else if(choice==2){
158                 searchName();
159             }
160             else if(choice==3){
161                 View();
162             }
163             else if (choice==4){
164                 deleteStu();
165             }
166             else if (choice==5){
167                 change();
168             }
169             else if (choice==0){
170                System.out.print("程序正在停止,请稍等");
171                 Robot  r   =   new   Robot();
172                for(int i=1;i<4;i++)
173                {
174                     r.delay(500);
175                    System.out.print(".");
176                }
177                 break;
178             }
179             else
180                 System.out.println("请输入上述选项!");
181         }
182 
183 
184 
185     }
186 }
 
原文地址:https://www.cnblogs.com/battlecry/p/9374497.html