一、数组
数组是一个固定长度的,包含了相同类型数据的 容器
int[] a; //声明数组
a = new int[5]; //创建一个长度是5的数组 让a这个引用,指向数组
访问数组:数组下标基0
.length属性,用于访问一个数组的长度
数组下表越界异常 ArrayIndexOutOfBoundsException
基础笔试面试题1
数组最小值:
首先创建一个长度是5的数组
然后给数组的每一位赋予随机整数
通过for循环,遍历数组,找出最小的一个值出来
0-100的 随机整数的获取办法有多种,下面是参考办法之一:
(int) (Math.random() * 100)
Math.random() 会得到一个0-1之间的随机浮点数,然后乘以100,并强转为整型即可。
public class shuzu {
public static void main(String[] args){
int a[] = new int[5];
a[0] = (int)(Math.random()*100);
a[1] = (int)(Math.random()*100);
a[2] = (int)(Math.random()*100);
a[3] = (int)(Math.random()*100);
a[4] = (int)(Math.random()*100);
System.out.println("数组里的随机数是");
for(int i=0;i<a.length;i++){
System.out.println("a["+i+"]"+"是"+a[i]);
}
int temp = a[0];
for(int j=0;j<a.length;j++){
if(a[j]<temp){
temp = a[j];
}
}
System.out.println(temp);
}
}
1、初始化数组
分配空间与赋值
public class HelloWorld {
public static void main(String[] args) {
//写法一: 分配空间同时赋值
int[] a = new int[]{100,102,444,836,3236};
//写法二: 省略了new int[],效果一样
int[] b = {100,102,444,836,3236};
//写法三:同时分配空间,和指定内容
//在这个例子里,长度是3,内容是5个,产生矛盾了
//所以如果指定了数组的内容,就不能同时设置数组的长度
int[] c = new int[3]{100,102,444,836,3236};
}
}
基础笔试面试题2
数组反转
import java.util.*;
public class shuzu2 {
public static void main(String[] args){
System.out.print("请输入数组的长度: ");
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int a[] = new int[num];
for(int i = 0;i<a.length;i++){
a[i] = (int) (Math.random()*100);
System.out.print(a[i]+" ");
}
int temp = 0;
for(int j=0;j<a.length/2;j++){
temp=a[a.length-(j+1)];
a[a.length-(j+1)]= a[j];
a[j] =temp;
}
int k=0;
System.out.println("反转后的数组:");
while(k<a.length){
System.out.print(a[k]+" ");
k+=1;
}
}
}
基础笔试面试题3
选择法排序:
选择法排序的思路:
把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来
比较完后,第一位就是最小的
然后再从第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来
比较完后,第二位就是第二小的
以此类推
public class HelloWorld {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
//排序前,先把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//选择法排序
//第一步: 把第一位和其他所有位进行比较
//如果发现其他位置的数据比第一位小,就进行交换
for (int i = 1; i < a.length; i++) {
if(a[i]<a[0]){
int temp = a[0];
a[0] = a[i];
a[i] = temp;
}
}
//把内容打印出来
//可以发现,最小的一个数,到了最前面
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//第二步: 把第二位的和剩下的所有位进行比较
for (int i = 2; i < a.length; i++) {
if(a[i]<a[1]){
int temp = a[1];
a[1] = a[i];
a[i] = temp;
}
}
//把内容打印出来
//可以发现,倒数第二小的数,到了第二个位置
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//可以发现一个规律
//移动的位置是从0 逐渐增加的
//所以可以在外面套一层循环
for (int j = 0; j < a.length-1; j++) {
for (int i = j+1; i < a.length; i++) {
if(a[i]<a[j]){
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
//把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
基础笔试面试题3
冒泡法排序:
冒泡法排序的思路:
第一步:从第一位开始,把相邻两位进行比较
如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的
第二步: 再来一次,只不过不用比较最后一位
以此类推
public class HelloWorld {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
//排序前,先把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//冒泡法排序
//第一步:从第一位开始,把相邻两位进行比较
//如果发现前面的比后面的大,就把大的数据交换在后面
for (int i = 0; i < a.length-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//把内容打印出来
//可以发现,最大的到了最后面
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//第二步: 再来一次,只不过不用比较最后一位
for (int i = 0; i < a.length-2; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//把内容打印出来
//可以发现,倒数第二大的到了倒数第二个位置
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
//可以发现一个规律
//后边界在收缩
//所以可以在外面套一层循环
for (int j = 0; j < a.length; j++) {
for (int i = 0; i < a.length-j-1; i++) {
if(a[i]>a[i+1]){
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
//把内容打印出来
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(" ");
}
}
基础笔试面试题4
增强for循环 (增强型for循环只能用来取值,却不能用来修改数组里的值)
public class HelloWorld {
public static void main(String[] args) {
int values [] = new int[]{18,62,68,82,65,9};
//常规遍历
for (int i = 0; i < values.length; i++) {
int each = values[i];
System.out.println(each);
}
//增强型for循环遍历
for (int each : values) {
System.out.println(each);
}
}
}
基础笔试面试题5
复制数组:
把一个数组的值,复制到另一个数组中
System.arraycopy(src, srcPos, dest, destPos, length)
src: 源数组
srcPos: 从源数组复制数据的起始位置
dest: 目标数组
destPos: 复制到目标数组的起始位置
length: 复制的长度
public class HelloWorld {
public static void main(String[] args) {
int a [] = new int[]{18,62,68,82,65,9};
int b[] = new int[3];//分配了长度是3的空间,但是没有赋值
//通过数组赋值把,a数组的前3位赋值到b数组
//方法一: for循环
for (int i = 0; i < b.length; i++) {
b[i] = a[i];
}
//方法二: System.arraycopy(src, srcPos, dest, destPos, length)
//src: 源数组
//srcPos: 从源数组复制数据的启始位置
//dest: 目标数组
//destPos: 复制到目标数组的启始位置
//length: 复制的长度
System.arraycopy(a, 0, b, 0, 3);
//把内容打印出来
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
}
2、二维数组
这是一个一维数组, 里面的每一个元素,都是一个基本类型int
int a[] =new int[]{1,2,3,4,5};
这是一个二维数组,里面的每一个元素,都是一个一维数组
所以二维数组又叫数组的数组
int b[][] = new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
};
初始化二维数组:
public class HelloWorld {
public static void main(String[] args) {
//初始化二维数组,
int[][] a = new int[2][3]; //有两个一维数组,每个一维数组的长度是3
a[1][2] = 5; //可以直接访问一维数组,因为已经分配了空间
//只分配了二维数组
int[][] b = new int[2][]; //有两个一维数组,每个一维数组的长度暂未分配
b[0] =new int[3]; //必须事先分配长度,才可以访问
b[0][2] = 5;
//指定内容的同时,分配空间
int[][] c = new int[][]{
{1,2,4},
{4,5},
{6,7,8,9}
};
}
}
3、Arrays
数组复制:
与使用System.arraycopy进行数组复制类似的, Arrays提供了一个copyOfRange方法进行数组复制。
不同的是System.arraycopy,需要事先准备好目标数组,并分配长度。 copyOfRange 只需要源数组就就可以了,通过返回值,就能够得到目标数组了。
除此之外,需要注意的是 copyOfRange 的第3个参数,表示源数组的结束位置,是取不到的。
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
// copyOfRange(int[] original, int from, int to)
// 第一个参数表示源数组
// 第二个参数表示开始位置(取得到)
// 第三个参数表示结束位置(取不到)
int[] b = Arrays.copyOfRange(a, 0, 3);
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
}
转换为字符串:
int
a[] =
new
int
[] {
18
,
62
,
68
,
82
,
65
,
9
};
String content = Arrays.toString(a);
排序:
int
a[] =
new
int
[] {
18
,
62
,
68
,
82
,
65
,
9
};
System.out.println(
"排序之前 :"
);
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(
"排序之后:"
);
System.out.println(Arrays.toString(a));
(二分)搜索:
查询元素出现的位置
需要注意的是,使用binarySearch进行查找之前,必须使用sort进行排序
如果数组中有多个相同的元素,查找结果是不确定的
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int a[] = new int[] { 18, 62, 68, 82, 65, 9 };
Arrays.sort(a);
System.out.println(Arrays.toString(a));
//使用binarySearch之前,必须先使用sort进行排序
System.out.println("数字 62出现的位置:"+Arrays.binarySearch(a, 62));
}
}
判断是否相同:
int
a[] =
new
int
[] {
18
,
62
,
68
,
82
,
65
,
9
};
int
b[] =
new
int
[] {
18
,
62
,
68
,
82
,
65
,
8
};
System.out.println(Arrays.equals(a, b));
填充:使用同一个值,填充整个数组
int
a[] =
new
int
[
10
];
Arrays.fill(a,
5
);
System.out.println(Arrays.toString(a));
基础笔试面试题6
二维数组排序:
首先定义一个5X8的二维数组,然后使用随机数填充满。
借助Arrays的方法对二维数组进行排序。
参考思路:
先把二维数组使用System.arraycopy进行数组复制到一个一维数组
然后使用sort进行排序
最后再复制回到二维数组。
关于随机数
0-100的 随机整数的获取办法有多种,下面是参考办法之一:
(int) (Math.random() * 100)
Math.random() 会得到一个0-1之间的随机浮点数,然后乘以100,并强转为整型即可。
import java.util.Arrays;
public class finArrays {
public static void main(String[] args){
int a[][] = new int[5][8];
int b[] = new int[40];
for(int i=0;i<a.length;i++){
for (int j=0;j<a[0].length;j++){
a[i][j] = (int)(Math.random()*100);
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(int i=0;i<a.length;i++){
System.arraycopy(a[i],0,b,a[0].length*i,a[0].length);
}
System.out.println("复制到一维数组:");
System.out.println(Arrays.toString(b));
Arrays.sort(b);
for(int i =0;i<a.length;i++){
System.arraycopy(b,8*i,a[i],0,a[0].length);
}
System.out.println("排序后的二维数组:");
for(int[] row:a){
for(int each:row){
System.out.print(each+" ");
}
System.out.println();
}
}
}
待练习:
合并数组
首先准备两个数组,他俩的长度是5-10之间的随机数,并使用随机数初始化这两个数组
(向数组填充随机数的办法)
然后准备第三个数组,第三个数组的长度是前两个的和
通过System.arraycopy 把前两个数组合并到第三个数组中