一维数组:
数组排序:Arrays.sort(数组名);(从小到大排序)
package com.demo.cn;
import java.util.Arrays;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {1,2,3,6,7,5,4};
Arrays.sort(arr); //从大到小排序
for(int i=0;i<arr.length;i++) //遍历
{
System.out.println(arr[i]);
}
}
}
冒泡排序
package com.demo.cn;
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {1,2,3,6,7,5,4};
for(int i=0;i<arr.length-1;i++) {
for(int j=0;j<arr.length-1-i;j++)
{
if(arr[j]<arr[j+1]) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
多维数组(二维数组,非重点)
概念:java中没有真正的多维数组,只是多个一维数组拼凑。(数组里面放数组)
命名规则:
类型 数组名【】【】
类型 【】【】数组名
类型 【】数组名【】(不推荐使用)
初始赋值:
int arr3【】【】=new int【3】【3】;
int arr4【】【】={{1,2,3},{4,5,6},{7,8,9}};
引用类型:
String str【】【】=new String{ {张三,李四},{王二麻子,小红}};
二维数组的遍历:
package com.demo.cn;
public class Demo4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[][]= {{1,2},{3,6},{7,5}};
for(int i=0;i<arr.length;i++) {
for (int j = 0; j < arr[j].length; j++) {
System.out.println(arr[i][j]);
}
}
}
}
Random ra=new Random();
int a=ra.nextInt(10); 生成0-9范围内的随机数(可在nextInt()+1(生成1-10));
arr3=Arrays.copyOf(arr3,arr3.length+1)(数组扩容1位);
System。out。println(Arrays。toString(arr3));(遍历数组arr3)
循环练习:
package com.demo1.cn;
public class Stident1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int max=11; //最大上半行行数
int row=1; //第row列行
int max_star=2*max-1; //最大*数
while(row<=max) { //从第一行到第11行循环
int star_con=2*row-1; //定义*的个数
int space_con=max-row; //定义空格的个数
int col=1;
while(col<=space_con) { //用while写出空格的个数
System.out.print(" ");
col++;
}
col=1;
while(col<=star_con) { //用while写出*的个数
System.out.print("*");
col++;
}
System.out.println(); //换行
row++;
}
row=1;
max=10;
while(row<=max) { //下半部分,与上半部分相似
int star_con=2*max-2*row+1;
int space_con=row-1;
int col=0;
while(col<=space_con) {
System.out.print(" ");
col++;
}
col=1;
while(col<=star_con) {
System.out.print("*");
col++;
}
System.out.println();
row++;
}
}
}
- 嵌套循环
1)//*
//**
//***
//输出50行
答案:
package com.demo1.cn;
public class Stident2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int row=1;
while(row<=50) {
int col=1;
while(col<=row) {
System.out.print("*");
col++;
}
System.out.println(" ");
row++;
}
}
}
2)
// *
// **
//***
//右对齐,50行
答案:
package com.demo1.cn;
public class Stident3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int row=1;
while(row<=50) {
int c=50;
while(c>row) {
System.out.print(" ");
c--;
}
int col=1;
while(col<=row) {
System.out.print("*");
col++;
}
System.out.println();
row++;
}
}
}
3)
*
***
*****
*******
//50行
package com.demo1.cn;
public class Stident4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int row=1;
int max=50;
int max_star=2*max-1;
while(row<=max) {
int star_cnt=2*row-1;
int space_cnt=max-row;
int col=1;
while(col<=space_cnt) {
System.out.print(" ");
col++;
}
col=1;
while(col<=star_cnt) {
System.out.print("*");
col++;
}
System.out.println();
row++;
}
}
}
数组练习:
第一题
// 1.两个数组长度都为6,里面放入随机数0—9
// 求出两个数组中的交集(第一个数组中有的数据第二个数组也有)
答案:
package com.demo1.cn;
import java.util.Arrays;
import java.util.Random;
public class Shuzu1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a1[]=new int[6]; //定义三个数组
int a2[]=new int[6];
int a3[]=new int [0];
Random r=new Random();
// ===========给a1和a2生产随机元素
for (int i = 0; i < a1.length; i++) {
a1[i]=r.nextInt(10);//0-9,10个元素
a2[i]=r.nextInt(10);//从0开始
}
//============遍历a1,a2
for (int i = 0; i < a1.length; i++) {
System.out.println(a1[i]);
}
System.out.println("---------------");
for (int i = 0; i < a2.length; i++) {
System.out.println(a2[i]);
}
System.out.println("a3============");
int num=0;
for (int i = 0; i < a1.length; i++) { //比较a1,a2数组內相同的元素并写入a3
for (int j = 0; j < a2.length; j++) { //数组中
if(a1[i]==a2[j]) {
num=a1[i];
a3=Arrays.copyOf(a3, a3.length+1); //用Arrays.copyOf()
a3[a3.length-1]=num; //函数拓展数组a3的长度
}
}
}
//============去除重复元素
int a4[]=new int [a3.length];
int t=0;
for (int i = 0; i < a3.length; i++) {
boolean bo=true;
for (int j = i+1; j < a3.length; j++) {
if(a3[i]==a3[j]) {
bo=false;
break;
}
}
if(bo) { //将a3数组中不重复的数写入a4数组中,并设置t长度
a4[t]=a3[i];
t++;
}
}
int a5[]=new int[t];
System.arraycopy(a4, 0, a5, 0, t); //将a4数组中的元素复制到a5中
System.out.println(""); //用System.arraycopy函数
System.out.println(Arrays.toString(a5));//用Arrays.toString()函数遍历数组a5
}
}
结果: