2020-03-07
接触Java的第二天,通过一段代码分析数据类型,数组等
代码分析
以下是一段可以随机生成一个复合术语名词的代码
public class jargon_machine {
public static void main(String[] args){
String[] wordListOne = {"24/7", "multi Tier","30000 ft","B to B", "win-win","dynamic","six sigma",
"smart", "critical path","pervasive", "passive", "static"};
String[] wordListTwo = {"empowered","centric","distributed","oriented","branded","leveraged",
"aligned","focused","cooperative","clustered","colored", "enormous"};
String[] wordListThree = {"process", "core competency","mission","portal","strategy","mindshare",
"architecture", "construction"};
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length;
int rand1 = (int)(Math.random() * oneLength);
int rand2 = (int)(Math.random() * twoLength);
int rand3 = (int)(Math.random() * threeLength);
String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
System.out.println("What we need is a: "+ phrase);
}
}
原理
- 创建包含String的数组Array,创建方式为:
String[] adj= {"excellent","awesome"}
String[] noun= {"fort","construction","maid"}
- 然后,为了在每个数组内调出一个元素,我们需要知道数组大小,用.length来赋值给变量
int number = adj.length
int number2 = noun.length
- 下一步为获取三个随机数,使用Math.random()方法,将获得一个介于0到1之间的小数,将此值乘以数组长度并取整
int random_number1 = (int)(Math.random() * number)
int random_number2 = (int)(Math.random() * number2)
- 最后便是从三个数组中随机挑选三个字符串后连接起来。使用索引index来提起数组内的元素
String jargon = adj[random_number1] + " " + noun[random_number2]
语法语句分析
变量
Java定义了4种变量:
- 实例变量Instance variable,也叫做非静态域Non static field: 对象将他们的状态存在非静态域内,也就是没有使用static关键词的域。 每一个对象的值都是独特的,所以叫做实例变量
- 类变量Class Variable,也叫做静态域 static fields: 使用static修饰任何变量/域告诉编译器不论类被实例化多少次,这个变量只有一个。
- 本地变量Local Variable,类似对象将状态存在域/变量内,方法一般会把他们的暂时状态temporary state存在本地变量内。本地变量仅存在于方法内,只要在方法的花括号内,便是本地变量。本地变量只能被创建的方法所见。
- 参数Parameter: 参数为传递给方法的信息,main方法中的args便是参数.
变量赋值
Java作为一个静态语言,所有的变量都需要先声明在使用,与python不同,java需要声明变量的类型与名称,首先来看数据类型:
Java主数据类型primitive type:
byte num1 = 1
short num2 = 30000
int num3 = 2147483647
long num4 = 9223372036854775807L
float num5 = 9.9999f
double num6 = 9.999999999999999d
boolean expression = true
char uniChar = 'C'
- byte, 8 bit,可存从-128 到127的整数
- short,16bit,可存从-32768到32767的整数
- int, 32bit,可存从 − 2 31 -2^{31} −231到 2 31 − 1 2^{31}-1 231−1的整数
- long,64bit,可存从 − 2 63 -2^{63} −263到 2 63 − 1 2^{63}-1 263−1的整数,以L结尾
- float 与double,32和64bit,若为了省内存,使用float,默认使用double,使用f和d结尾,具体详见 Java documentation
- boolean, 布尔值,true或者false
- char,一般用于储存单个unicode字符,从’\u0000’ (0) 到 ‘\uffff’ (65,535), 也可使用ASCII值
因为primitive type为Java内置的数据类型,而非从类内创建的对象,所以创建新的变量并赋值时不需要用new关键词。
我们称某个固定值的源码表达形式(source code representation of a fixed value)为Literal,而literal可直接赋值给primitive数据类型。
- Integer Literals: 表达整数的literal,可使用10进制,16进制(0x)和2进制(0b)
- Float-point Literals: 表达小数,可使用科学计数法E来写
- Character or String Literals:使用单引号代表单个字符,双引号代表字符串
- Class Literals: 将类名加.class,i.e.
Class<String> = String.class
变量赋值方式
- 使用literal赋值
int size = 32
- 指派其他变量的值
int otherSize = size
- 以上结合
int totalSize = size + 32
数组Array
Java的数组可以理解为python的list,但仅限于一个数据类型。
在数组内,每一个索引可存一个数据,数组内的数据被称为元素element, 数组为0-based,比如数组内第九个数据在索引index 8的位置
数组创建
类似创建变量,创建数组时也需要名称以及类型,使用[方括号] 在类型后来声明数组的变量类型, 如下:
//declarae arrays of some primitive type (more will apply)
int[] anArray;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
以上代码其实并未创建数组,仅是告诉编译器哪种数据类型可以放进对应的数组内。
我们需要创建并初始化initialize数组:
anArray = new int[10]; //create an array of integers
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // and so forth
比较简便的方法便是如下,直接创建并赋值
String[] wordListThree = {"process", "core competency","mission","portal","strategy","mindshare","architecture", "construction"};
数组的长度为固定值,创建后不能更改,但是元素可更改.
复制数组
复制数组使用的是arraycopy方法
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
代码:
class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7);
System.out.println(new String(copyTo));
}
}
// The output is caffein
解析:
- 创建了一个叫做copyFrom的char数组,包含14个字符
- 创建叫copyTo的char数组,初始化7个位置
- 使用arraycopy,从copyFrom数组的第2位开始复制,从compyTo数组的第0为开始粘贴,持续7个数
更简洁的复制
使用API内的java.util.Array类提供的copyOfRange方法,如下
class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9);
System.out.println(new String(copyTo));
}
}
// The output is caffein
java.util.Arrays.copyOfRange(copyFrom, 2, 9)
复制copyFrom的第2位到第8位 - exclusive
Reference
Java documentation Array
Stack Overflow class literal