**最近初学JAVASE,写一下知识点,分享个大家。 **
循环
平时主要就是用到 for循环 和 while循环
for 循环:
for (int i = 0;i < 10;i++){
....
}
while 循环:
while (true){
....
}
数组
数组是非常重要的变量类型:
数组基本要素:
标识符:数组的名称,用于区分不同的数组
数组元素:向数组中存放的数据
元素下标:对数组元素进行编号,从0开始,数组中的每个元素都可以通过下标来访问
元素类型:数组元素的数据类型
数组语法:
int[ ] score1; //Java成绩
int score2[ ]; //C#成绩
String[ ] name; //学生姓名
不能同时设置数组大小又往里赋值
int[ ] score = {89, 79, 76};
int[ ] score = new int[ ]{89, 79, 76};
int[ ] score = new int[ 3 ]{89, 79, 76}//这是错误的。。。
数组的小李子:
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// 打印所有数组元素
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// 计算所有元素的总和
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// 查找最大元素