数组
数组的声明
一维数组
- 先声明,再用new进行内存分配
数组元素类型 数组名字[ ];
数组元素类型[ ] 数组名字; - 声明的同时为数组分配空间
数组元素类型 数组名 = new 数组元素类型[数组元素的个数]
二维数组
- 先声明,再用new进行内存分配
数组元素类型 数组名字[ ][ ];
数组元素类型[ ][ ] 数组名字;
数组元素类型[ ] 数组名字[ ];
a=new int[2][4]; - 声明的同时为数组分配空间
a=new int[2][];
a[0]=new int[2];
a[1]=new int[4];
数组的初始化
一维数组
int arr[]=new int[]{1,2,6,};
int arr[]={1,2,6};
二维数组
int arr[] []={{1,2,3}{4,5,6}{7,8,9}};
数组的使用
生兔子的问题可以用数组来解决更简单
/*
* 生兔子问题
*/
public class demo01 {
public static void main(String[] args) {
int[] tuzi=new int[20];
tuzi[0]=1;
tuzi[1]=2;
tuzi[2]=3;
for (int i = 3; i < tuzi.length; i++) {
tuzi[i]=tuzi[i-3]+tuzi[i-1];
}
System.out.println(tuzi[19]);
}
}
利用数组,求平均分
/*
* 利用数组,求平均分
*/
import java.util.Scanner;
public class demo02 {
public static void main(String[] args) {
int[] score = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < score.length; i++) {
score[i] = sc.nextInt();
}
float avg = 0;
float sum = 0;
for (int i = 0; i < score.length; i++) {
sum = sum + score[i];
}
avg = sum / score.length;
System.out.println("平均分为:" + avg);
}
}
冒泡排序
/*
*冒泡排序
*/
public class demo03 {
public static void main(String[] args) {
int[] score = { 62, 95, 87, 89, 71 };
int temp = 0;
for (int i = 0; i < score.length; i++) {
for (int j = 0; j < score.length - i - 1; j++) {
//比较第i次,剩余数组的长度等于score.leng-i
if (score[j] > score[j + 1]) {
temp = score[j + 1];
score[j + 1] = score[j];
score[j] = temp;
}
}
}
for(int i=0;i<score.length;i++){//循环输出已经排好的数组
System.out.println(score[i]);
}
}
}
生成随机数,并排序
/*
* 利用电脑随机生成10个两位数的随机数,并按升序排序
*/
import java.util.Random;
public class demo07 {
public static void main(String[] args) {
int a[] = new int[10];
Random r = new Random();
for (int i = 0; i <= 9; i++) {
a[i] = r.nextInt(90)+10;
System.out.print(a[i]+" ");
}
System.out.println();
int temp = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;//冒泡排序法
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] +" ");
}
}
}
java.util.Arrays类
- sort()方法:进行升序排序
Arrays.sort(数组名); - copyOf()方法:进行数组元素的复制
Arrays.copyOf(数组名,int数字)数字代表复制的长度
Arrays.copyOfRange(数组名,int开始,int结束)
(int开始)<=X<(int结束) - toString()方法:转换为字符串
Arrays.toString(数组名)
import java.util.Arrays;
public class demo01 {
public static void main(String[] args) {
int[] a = { 12, 45, 84, 65, 75 };
int[] b = Arrays.copyOf(a, 3);//3为copy的个数,下标从0开始
int[] c = Arrays.copyOfRange(a, 1, 3);//1为copy开始的下标,3为结束的下标,且1<=copy<3
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+" ");
}
System.out.println();
for (int i = 0; i < c.length; i++) {
System.out.print(c[i]+" ");
}
}
}
输出一个九宫格
/*
* 输出一个九宫图
*/
public class demo02 {
public static void main(String[] args) {
int[][] a = new int[3][];// 必须填写左边的数,表示有几行
System.out.println(a[0]);//二维数组在声明后,a[0]为地址,且为null。
a[0] = new int[] { 4, 9, 2 };
a[1] = new int[] { 3, 5, 7 };
a[2] = new int[] { 8, 1, 6 };// 给二维数组赋值
//int[][] b = new int[][] { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } };// 给二维数组赋值
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println("");
}
}
}
打印一个4X4的矩阵
import java.util.Random;
import java.util.Scanner;
/*
* 用电脑生成随机数,打印出一个4X4的矩阵,并输出两个对角线的成绩
*/
public class demo01 {
public static void main(String[] args) {
Random r = new Random();
Scanner sc = new Scanner(System.in);
int MAX = sc.nextInt();
int arr[][] = new int[MAX][MAX];
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
arr[i][j] = r.nextInt(9) + 1;
}
}
for (int i = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
int product1 = 1;// 主对角线的乘积
int product2 = 1;// 副对角线的乘积
for (int i = 0; i < MAX; i++) {
product1 = product1 * arr[i][MAX - i - 1];
product2 = product2 * arr[i][i];
}
System.out.println("主对角线的乘积为:" + product1);
System.out.println("副对角线的乘积为:" + product2);
}
}
字符串
字符串的声明与赋值
- String s=new String(“abc”);
在栈内存中开辟地址指向堆内存,在堆内存存放“abc”。 - String s =”abc”;
先在常量池中找“abc”,若没有则独立开辟内存空间存放“abc”。
字符串的使用
String类的方法:
1. s.length():返回字符串的长度
2. s.equals():比较两个字符串对象的内容是否一致
3. s.concat():连接两个字符串,与“+”相同
4. s.indexOf(char):返回第一个出现’char’的索引
5. s.lastIndexOf(char):返回最后一个出现’char’的索引
6. s.subString(int index):提取从索引位置开始的字符串
7. s.subString(int beginindex,int endindex):提取两个索引之间的字符串【注意:(beginindex)<=输出部分<(endindex)】
8. s.split(“char”):将字符串按 “char”分割为子字符串,结果作为字符串数组返回
9. s.compareTo():字符串的比较
大小写字母转换
/*
* 将一个字符串中的小写字母转换为大写字母,大写字母转换为小写字母输出
*/
public class demo04 {
public static void main(String[] args) {
String s = "dsfsGDFGFdsfsdFDSFs";
String s1 = "";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 'a' && c <= 'z') {
c = (char) (c - 32);
} else if (c >= 'A' && c <= 'Z') {
c = (char) (c + 32);
}
s1 = s1 + c;
}
System.out.println(s1);
}
}
另一种方法用Chracter类里边方法
/*
* 将一个字符串中的小写字母转换为大写字母,大写字母转换为小写字母输出
* 使用Character类里边的方法
*/
public class demo03 {
public static void main(String[] args) {
String s = "dsfsGDFGFdsfsdFDSFs";
String s1 = "";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isLowerCase(c)) {
c = Character.toUpperCase(c);
} else {
c = Character.toLowerCase(c);
}
s1 = s1 + c;
}
System.out.println(s1);
}
}
检验邮箱格式
import java.util.Scanner;
/*
* 判断一个字符串是否为邮箱的正确格式
*/
public class demo05 {
public static void main(String[] args) {
System.out.println("请输入您的邮箱:");
Scanner sc = new Scanner(System.in);
String s = sc.next();
boolean isEmail = false;
if (s.contains("@")) {
if (s.indexOf("@") == s.lastIndexOf("@")) {
if (s.endsWith(".com") || s.endsWith(".cn") || s.endsWith(".net")) {
String s1 = s.substring(0, s.indexOf("@"));
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
if (Character.isLetter(c) || c == '_' || Character.isDigit(c)) {
String s2 = s.substring(s.indexOf("@") + 1, s.indexOf("."));
for (int j = 0; j < s2.length(); j++) {
char c1 = s2.charAt(j);
if (Character.isLetter(c) || c == '_' || Character.isDigit(c)) {
isEmail = true;
}
}
}
}
}
}
}
if (isEmail) {
System.out.println("您好,你输入的邮箱合法!");
} else {
System.out.println("对不起,您输入的邮箱不合法!");
}
}
}
给字符串插入字符
StringBuffer类和StringBuilder类的使用
它们是可变的,不会在字符串常量池中,而是在堆中。每个StringBuffer对象都有一定容量,如果字符串长度没有超过此容量,就无需分配新的内存缓冲区,如果溢出,则此容量自动增大。
/*
* 给一个多位数字字符串,每隔3个数字,插入一个逗号,并输出!
*/
public class demo06 {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("123456789123456789");
for (int i = 3; i < s.length(); i += 4) {
s.insert(i, ",");
}
System.out.println(s);
}
}