这篇文章作为我学习的一个记录, 起到一个督促作用, 重新捡捡Java的语法, 这是第一篇. 适合新新手.
这篇跟以前我入门Swift语言的文章很类似, 之后会逐渐逐渐丰富内容
import java.util.Arrays;
public class JavaTest {
// 这里定义一个函数 参数为数组, 没有返回值
public void printArray(double[] arrayList) {
for(double obj: arrayList) {
System.out.print(obj + " ");
}
}
// 这是一个有返回值的函数, 参数为一个数组
public double[] reArray(double[] arrayList) {
double[] res = new double[arrayList.length];
for (int i = 0, j = res.length - 1; i < arrayList.length; i ++, j --) {
res[j] = arrayList[i];
}
return res;
}
// main
public static void main(String[] args) {
// TODO Auto-generated method stub
// 定义一个字符数组
char[] helloArray = { 'a', 'b', 'c', 'd'};
String helloString = new String(helloArray);
// 输出
System.out.println(helloString);
// 字符串长度
String site = "summerxx.com";
int len = site.length();
System.out.println(len);
// 字符串连接
System.out.println("My name is : ".concat("summerxx"));
System.out.println("My name is : "+ "summerxx"+ " is jianshu Author");
// 创建格式化字符串
int i = 12;
String s = "summerxx";
System.out.printf("浮点型变量的值: " + "%s" + "整形变量的值: " + "%d", s, i);
System.out.print("\n");
// 格式化字符串
String fs;
fs = String.format("%s" + "%d", s, i);
System.out.println("格式化的值fs = " + fs); // summerxx12
// 更多API请看
// http://www.runoob.com/manual/jdk1.6/java/lang/String.html
// Java StringBuffer 和 StringBuilder 类
// 这里有点类似Swift的数组
StringBuffer sBuffer = new StringBuffer("夏天Java简单学习 ");
// 追加方法
sBuffer.append(" 第一步学一下Java语法");
sBuffer.append(" Java数据类型");
sBuffer.append(" Java的简单理解");
System.out.println(sBuffer); // 夏天Java简单学习 第一步学一下Java语法 java 数据类型 java 的简单理解
// 字符串反转
System.out.println(sBuffer.reverse());
// 移除 0-4
System.out.println(sBuffer.delete(0, 4));
// 插入
System.out.println(sBuffer.insert(0, 4444)); // 4444的avaJ 型类据数avaJ 法语avaJ下一学步一第 习学单简avaJ天夏
// 替代
System.out.println(sBuffer.replace(0, 4, "8888")); // 8888的avaJ 型类据数avaJ 法语avaJ下一学步一第 习学单简avaJ天夏
//
System.out.println(sBuffer.length());
//
System.out.println(sBuffer.charAt(0)); // 8
/*
1 int capacity()
返回当前容量。
2 char charAt(int index)
返回此序列中指定索引处的 char 值。
3 void ensureCapacity(int minimumCapacity)
确保容量至少等于指定的最小值。
4 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此序列复制到目标字符数组 dst。
5 int indexOf(String str)
返回第一次出现的指定子字符串在该字符串中的索引。
6 int indexOf(String str, int fromIndex)
从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。
7 int lastIndexOf(String str)
返回最右边出现的指定子字符串在此字符串中的索引。
8 int lastIndexOf(String str, int fromIndex)
返回最后一次出现的指定子字符串在此字符串中的索引。
9 int length()
返回长度(字符数)。
10 void setCharAt(int index, char ch)
将给定索引处的字符设置为 ch。
11 void setLength(int newLength)
设置字符序列的长度。
12 CharSequence subSequence(int start, int end)
返回一个新的字符序列,该字符序列是此序列的子序列。
13 String substring(int start)
返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
14 String substring(int start, int end)
返回一个新的 String,它包含此序列当前所包含的字符子序列。
15 String toString()
返回此序列中数据的字符串表示形式。
*/
// Java 数组
double[] arrayList = new double[5];
arrayList[0] = 6.6;
arrayList[1] = 7.7;
arrayList[2] = 5.6;
arrayList[3] = 88.7;
arrayList[4] = 996;
// 简单计算
double total = 0.0;
for (int j = 0; j < arrayList.length; j ++) {
total += arrayList[j];
}
System.out.println("总和是 : "+ total);
// 数组的简单处理
// 查找最大元素
double max = arrayList[0];
for (int k = 1; k < arrayList.length; k++) {
if (arrayList[k] > max) max = arrayList[k];
}
System.out.println("Max is " + max);
for (double ele: arrayList) {
System.out.println("遍历输出每个元素"+ ele);
}
// 创建一个实例
// 调用一个公有方法
JavaTest jObj = new JavaTest();
// 数组作为函数参数
jObj.printArray(arrayList);
System.out.println("");
// 数组作为返回值赋值给数组
double[] re = jObj.reArray(arrayList);
for(double ele: re) {
System.out.println(ele);
}
// Java也有多维数组的概念, 数组作为数组元素.
// Arrays类, 使用之前需要引入import java.util.Arrays;
int[] arrays = new int[5];
Arrays.fill(arrays, 5);
System.out.println("数组的第一个元素"+ arrays[0]);
/* 给数组赋值:通过 fill 方法。
对数组排序:通过 sort 方法,按升序。
比较数组:通过 equals 方法比较数组中元素值是否相等。
查找数组元素:通过 binarySearch 方法能对排序好的数组进行二分查找法操作。
*/
}
}
一起学习 共同进步 走过一段有意义的时光
我是夏天 这是我维护的群 498143780 你可以来找我交流.