Random和ArrayList的应用
/*Random类应用与Math类应用,创建一个类,
* 1)分别用Random类和Math.random()方法生成随机数。
* 2) 把Math.random()方法生成的随机数,转换成1-100的整数
*
*/
public class Test { public static void main(String[] args)
{
//创建一个Random类对象
Random rand = new Random(); //随机生成20个随机整数,并将其显示出来
for (int i = 0; i < 10; i++)
{
int num = rand.nextInt();
System.out.println("Random类生成的第" + (i + 1) + "个随机数是:" + num);
}
//用Math.random()随机生成20个随机数,并将其显示出来
for (int i = 0; i < 10; i++)
{
double num = Math.random();
System.out.println("Math.random()生成的第" + (i + 1) + "个随机数是:" + num);
System.out.println("转换成1-100的整数:"+(int)(num*100));
}
}
}
/*ArrayList类应用 ,创建一个类,把0到9的10个整数定义为包装类对象,并存放到ArrayList中,
* 1) 输出数组的长度;
* 2) 输出数组的元素;
* 3) 输出效果图如下
*
*/
public class Test { public static void main(String[] args) {
ArrayList al = new ArrayList(); //创建一个空ArrayList对象
for (int i = 0; i < 10; i++) {
Integer num = new Integer(i); //创建整型包装类对象
al.add(num); //将该对象存放到ArrayList中
}
System.out.println("数组中的长度:"+al.size()); System.out.println("数组中的元素:");
for (int i = 0; i < al.size(); i++) {
Integer temp = (Integer)(al.get(i)); //获得ArrayList中索引为i的元素
System.out.print(temp+","); }
System.out.println();
System.out.println("**************************");
al.clear(); //清空
System.out.println("数组被清空后的情况:");
System.out.println("数组长度为:" + al.size());
if (al.isEmpty()) { //判断是否为空
System.out.println("数组现在为空。");
}
else {
System.out.println("数组现在不为空。");
}
}
}
/*ArrayList类应用 ,创建一个动态数组,往动态数组中添加5种水果元素,“”,
* 1) 输出数组的长度;
* 2) 输出数组的所有元素的值;
* 3) 判断西瓜是否存在数组中,若存在,索引位置为多少
* 4)删除索引3的元素后,并输出剩下的所有元素值。
*
*/
public class Test { public static void main(String[] args) {
ArrayList al = new ArrayList(); //创建一个空的ArrayList对象
//往动态数组中添加元素
al.add("苹果"); al.add("梨子"); al.add("香蕉");
al.add("西瓜"); al.add("榴莲");
System.out.println("目前数组的长度:" + al.size());
for (int i = 0; i < al.size(); i++) {
System.out.println((String)(al.get(i)));
}
String str = new String("西瓜");
int index = al.indexOf(str); //判断某个元素是否存在
if (index < 0) {
System.out.println(str + "在数组中不存在。");
} else {
System.out.println(str + "存在,索引为:" + index);
}
al.remove(3); //删除某个索引位置的元素
System.out.println("删除索引为3的元素后的情况:");
for (int i = 0; i < al.size(); i++) {
System.out.println((String)(al.get(i)));
}
}
}
Random和ArrayList的应用的更多相关文章
- API(Scanner、Random、ArrayList、String、Arrays、Math)
Scanner import java.util.Scanner; /* public int nextInt(): to get a integer from keyboard public Str ...
- API之Scanner,Random,ArrayList基础运用。重点是ArrayList
有关API的这些类可以参考JDK的官方中文文档,看我的另一篇文章有下载==> https://www.cnblogs.com/gz18221/p/11968505.html<==文章地址 ...
- 常用API - Scanner、Random、ArrayList
API 概述 API(Application Programming Interface),应用程序编程接口. Java API是一本程序员的 字典 ,是JDK中提供给我们使用的类的说明文档. 这些类 ...
- 01 语言基础+高级:1-3 常用API第一部分_day07【Scanner类、Random类、ArrayList类】
day07[Scanner类.Random类.ArrayList类] Scanner类Random类ArrayList类 教学目标 能够明确API的使用步骤能够使用Scanner类获得键盘录入数据能够 ...
- Java: Difference between ArrayList and LinkedList
Basically, they are just two different implementations of List interface. LinkedList is implemented ...
- 类ArrayList
什么是ArrayList类 Java提供了一个容器 java.util.ArrayList 集合类,他是大小可变的数组的实现,存储在内的数据称为元素.此类提供一些方法来操作内部存储的元素. Array ...
- Java中ArrayList类
ArratList 类:存放同一数据类型容器(只能为引用数据类型,因实际其内部存放的是地址) 1.导入其所在包 import java.util.ArratList 2.创建对象 ArrayList& ...
- 如何把数值或者对象添加到ArrayList集合
生成6个1~33之间的随机整数,添加到集合,并遍历 public class ArrayListDemo1 { public static void main(String[] args) { // ...
- Java之ArrayList类(集合)
集合的由来 我们想存储多个数据,选择的容器可以是数组.而数组的长度是固定的,无法适应数据变化的需求.为了解决这个问题,Java提供了另一个容器 java.util.ArrayList 集合类,让我们可 ...
随机推荐
- 杂项-Java:MyBatis
ylbtech-杂项-Java:MyBatis 1.返回顶部 1. MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundatio ...
- Shell脚本下条件测试(eq.ne.....)(转载)
转载:http://cxj632840815.blog.51cto.com/3511863/1168709 Shell编程中的条件测试 在Linux编程中经常会用到判断数值的大小,字符串是否为空这样或 ...
- SQL Server 添加描述
添加描述的格式 exec sys.sp_addextendedproperty @name = N'MS_Description' ,@value = 'value',@level0type=N'SC ...
- MySQL基础 — 详细安装
MySQL--安装 打开MySQL 5.5 安装文件开始: 点击Next 打上勾,再点击Next 点击Custom,说明如下: Typical(典型安装) Installs the mo ...
- SVN安装失败提示
svnserve: error while loading shared libraries: libaprutil-1.so.0: cannot open shared object file: 1 ...
- 实现grep命令
#include <stdio.h> #include <string.h> #include <stdlib.h> // grep命令:grep match_pa ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- ASP.NET Core&EF 笔记
首先创建Asp.net Core项目,然后通过 NuGet 安装 EntityFrameworkCore: Microsoft.EntityFrameworkCore.SqlServer Micros ...
- SQL编程语句
视图 视图就是我们查询出来的虚拟表创建视图:create view 视图名 as SQL查询语句,分组,排序,in 等都不能写视图的用法: select * from 视图名 SQL编程 定义变量:d ...