数组
1. 数组静态初始化的三种形式
int[] arr= {4,2,7,5};
int[] arr = new int[]{4,2,7,5};
Object[] arr;
arr=new String[]{“hello”,”world”};
2. 有一个数组 String[] s=new String[]{“hello”,“word”,"!"}; 使用不同的方式进行遍历
public class Test1 {
public static void main(String[] args) {
//方式一:for循环
String[] s = new String[] {"hello","world","!"};
for(int i=0;i<s.length;i++) {
System.out.println(s[i]);
}
//方式二:增强for循环
for(String s1:s) {
System.out.println(s1);
}
}
}
关于length:String类有一个length()方法,数组的length是它的属性,集合的元素个数通过调用size()方法来获得。
3. 设计一个方法求一组数的平均值和最大值
public class Test2 {
//求一组数中的最大值
public int max(int[] arr) {
int max=arr[0];
for(int i=1;i<arr.length;i++) {
if(max<arr[i]) {
max=arr[i];
}
}
return max;
}
//求一组数的平均值
public double avg(int[] arr) {
int sum =0;
double avg;
for(int i=0;i<arr.length;i++) {
sum+=arr[i];
}
avg = sum/arr.length;
return avg;
}
public static void main(String[] args) {
Test2 t = new Test2();
int[] arr= {1,5,4,6,7,2};
int max = t.max(arr);
double avg = t.avg(arr);
System.out.println("这个数组的最大值为:"+max);
System.out.println("这个数组的平均值为:"+avg);
}
}
4. 定义一个二维数组int[][],遍历该二维数组。
public class Test3{
public static void main(String[] args) {
int[][] arr = {{1,2,3},
{4,5,6},
{7,8,9}};
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
System.out.println(arr[i][j]);
}
}
}
}
随机数
1. 产生10个随机数,范围在[1,100]使用Math.random()来实现
public class Test4 {
public static void main(String[] args) {
int[] arr = new int[10];
for(int i=0;i<10;i++) {
arr[i]=(int)((Math.random()*100+1));
System.out.println("第"+(i+1)+"个随机数是"+arr[i]);
}
}
}
Math.random()*100求取的是0.0到无限趋近于100.0之间的浮点数,(int)(Math.random()*100)求取的是1到99之间的99个整型数,所以要加上1才取得到100。
2. 随机产生一个长度为4位的验证码,包含大小写字母以及数字
public class Test5 {
public static void main(String[] args) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char[] arr = str.toCharArray();
char[] c = new char[4];
for(int i=0;i<c.length;i++) {
c[i]=arr[(int) (Math.random()*arr.length)];
System.out.println(c[i]);
}
String s = String.valueOf(c);
System.out.println("长度为4位的验证码是"+s);
}
}
toCharArray()方法是属于java.lang包下的String类,是把字符串转换成字符数组的方法;valueOf()也是String类的一个方法,作用是获取数组的值。
3. 有一组手机号,随机抽取一个中奖号码
public class Test6 {
public static void main(String[] args) {
String[] str = {"110","119","120"};
int i=(int) (Math.random()*str.length);
System.out.println("恭喜"+str[i]+"中奖了");
}
}
4. 程序随机生成一个20个1-100整数数据的数组,然后输入要查找的数据,如果数据存在返回数据所在的位置,如果不存在打印不存在
public class Test7 {
public static void main(String[] args) {
int[] arr = new int[20];
for(int i=0;i<arr.length;i++) {
arr[i]= (int) (Math.random()*100+1);
System.out.println(arr[i]);
}
System.out.println("请输入你想要查找的数:");
int i= new Scanner(System.in).nextInt();
int index =-1;
for(int j=0;j<arr.length;j++) {
if(i==arr[j]) {
index =j;
System.out.println(i+"的值在数组中的位置为"+(index+1));
}
}
if(index < 0){
System.out.println("输入的值" + i + "没有找到");
}
}
}
5. 产生100个[1,6]之间的随机数,利用频率数组统计每个数字出现的次数
public class Test8 {
public static void main(String[] args) {
int[] arr = new int[6];
int[] arr1= new int[100];
for(int i=0;i<arr1.length;i++) {
arr1[i]=(int) (Math.random()*6+1);
System.out.print(arr1[i]);
if((i+1)%10==0) {
System.out.println();
}
arr[arr1[i]-1]++;
}
for(int i=0;i<6;i++) {
System.out.println("出现数字"+(i+1)+"的次数为"+arr[i]);
}
}
}
算法题
1. 冒泡排序
public class Test9 {
public static void main(String[] args) {
int[] arr = { 4, 1, 3, 2 };
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
2. 插入排序
public class InsertSort{
public static void main(String[] args) {
int[] a= {19,3,56,8,55,89,1};
//循环每一个数
for(int i=0;i<a.length;i++) {
//将a[i]向前移动,移动到前面一个值比他小或者是前面没有值就停下来
//因为是向前移动,所以j是--的
for(int j=i;j>0;j--) {
if(a[j]<a[j-1]) {
int tmp=a[j];
a[j]=a[j-1];
a[j-1]=tmp;
}else {
//如果值和他相等或者是比他小的话,就可以不用再往前移动了
break;
}
}
}
System.out.println("新数组为"+Arrays.toString(a));
}
}
3. 选择排序
/*
* 选择排序:从第一个位置开始,这个位置与他后面所有的位置上面的数字比较。如果不满足就调换位置
*/
public class OptSort {
public static void main(String[] args) {
int[] arr = {80,12,9,45,66};
for(int j=0;j<arr.length;j++) {
for(int i=j;i<arr.length;i++) {
if(arr[j]>arr[i]) {
int tmp = arr[j];
arr[j]=arr[i];
arr[i]=tmp;
}
}
}
System.out.println("新数组是"+Arrays.toString(arr));
}
}