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 集合类,让我们可 ...
随机推荐
- codeforces 245H Queries for Number of Palindromes RK Hash + dp
H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...
- linux驱动编写(Kconfig文件和Makefile文件)
在Linux编写驱动的过程中,有两个文件是我们必须要了解和知晓的.这其中,一个是Kconfig文件,另外一个是Makefile文件.如果大家比较熟悉的话,那么肯定对内核编译需要的.config文件不陌 ...
- 从csv文件读取数据到二维vector
void ReadDataFromCsv(std::string &filename, std::vector<std::vector<std::string> > & ...
- Python网咯爬虫 — Scrapy框架应用
Scrapy框架 Scrapy是一个高级的爬虫框架,它不仅包括了爬虫的特征,还可以方便地将爬虫数据保存到CSV.Json等文件中. Scrapy用途广泛,可以用于数据挖掘.监测 ...
- HTML5常用知识点
github代码:https://github.com/showkawa/H5C3/tree/master/html5 1.自定义属性 data- 1.1 可以给html里的所有DOM对象都可以添加一 ...
- Java实现短信中提取号码
Description 提取一条短信里所有的电话号码,电话号码之间换行打印,短信的内容由用户输入. Input 第一行有个整数n(1≤n≤1000)表示测试用例的个数.其后的每一行中有一条短信,每一条 ...
- [Offer收割]编程练习赛84 -- 括号序列
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个只包含'(', ')'和''的字符串S,现在小Hi可以任意指定''为'('或')',不同的'*'可以是不同的字符. ...
- magento “Model collection resource name is not defined” 错误
问题出现于使用Grid时,解决方案.在使用的Model处添加 public function _construct() { parent::_construct(); $this->_init( ...
- Linux重启和关机命令
Linux重启命令: 方式1:shutdown –r now 方式2:reboot Linux关机命令: shutdown –h now
- 398 Random Pick Index 随机数索引
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...