int a = 10; Integer integer = new Integer(a); int b = integer.intValue(); System.out.println(a = b);
int[] arr = new int[5]; arr[0] = 1; arr[1] = 2;
int[] arr = new int[]{3,5,1,7};
int[] arr = {3,5,1,7};
int[]arr = { 1, 2, 3, 4, 5 };
int[] arr2 = new int[] { 1, 2, 3, 4, 5 };
int[] arr3=new int[3];
arr3[0]=1;
arr3[1]=5;
arr3[2]=6;
int[] arr;
arr={1,2,3,4,5};
int[] arr={1,2,3,4,5};
public static void main(String[] args) {
int[] x = { 1, 2, 3 };
for (int y = 0; y < 3; y++) {
System.out.println(x[y]);
// System.out.println("x["+y+"]="+x[y]); 打印效果 x[0]=1;
} // 那么这就是数组的第一个常见操作.遍历
}
public static void main(String[] args) {
int[] x = { 1, 2, 3 };
for (int y = 0; y < x.length; y++) {
System.out.println(x[y]);
// System.out.println("x["+y+"]="+x[y]); 打印效果 x[0]=1;
} // 那么这就是数组的第一个常见操作.遍历
}
public static void main(String[] args) {
int[] x = { 1, 2, 3 };
System.out.println(x[3]);
//java.lang.ArrayIndexOutOfBoundsException
}
public static void main(String[] args) {
int[] x = { 1, 2, 3 };
x = null;
System.out.println(x[1]);
// java.lang.NullPointerException
}
*/
public static int getMax(int[] arr)
{
//定义变量记录较大的值,初始化为数组中的任意一个元素。
int max = arr[0];
for(int x=1; x<arr.length; x++)
{
if(arr[x]>max)
max = arr[x];
}
return max;
}
*/
public static void selectSort(int[] arr)
{
for(int x=0; x<arr.length-1; x++)
{
for(int y=x+1; y<arr.length; y++)//为什么y的初始化值是 x+1?因为每一次比较,
//都用x角标上的元素和下一个元素进行比较。
{
if(arr[x]>arr[y])
{
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
/*
冒泡排序。
比较方式:相邻两个元素进行比较。如果满足条件就进行位置置换。
原理:内循环结束一次,最值出现在尾角标位置。
*/
public static void bubbleSort(int[] arr)
{
for(int x=0; x<arr.length-1; x++)
{
for(int y=0; y<arr.length-x-1; y++)//-x:让每次参与比较的元减。
//-1:避免角标越界。
{
if(arr[y]>arr[y+1])
{
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
}
}
}
/*
为了提高查找效率,可使用折半查找的方式,注意:这种查找只对有序的数组有效。
这种方式也成为二分查找法。
*/
public static int halfSeach(int[] arr,int key)
{
int min,mid,max;
min = 0;
max = arr.length-1;
mid = (max+min)/2;
while(arr[mid]!=key)
{
if(key>arr[mid])
min = mid + 1;
else if(key<arr[mid])
max = mid - 1;
if(min>max)
return -1;
mid = (max+min)/2;
}
return mid;
}
/*
反转其实就是头角标和尾角标的元素进行位置的置换,
然后在让头角标自增。尾角标自减。
当头角标<尾角标时,可以进行置换的动作。
*/
public static void reverseArray(int[] arr)
{
for(int start=0,end=arr.length-1; start<end; start++,end--)
{
swap(arr,start,end);
}
}
//对数组的元素进行位置的置换。
public static void swap(int[] arr,int a,int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
class Demo3
{
// 定义一个遍历二维数组的功能函数
public static void printArr2( int [][] a ){
// 1. 拆开二维数组
for ( int i = 0 ; i < a.length ; i++ )
{
// 2. 拆开一维数组获取数据
for ( int j = 0 ; j < a[i].length ; j++ )
{
System.out.print( a[i][j]+" ," );
}
}
}
// 定义一个函数计算二维数组中的元素的累加和
public static long getSum( int [][] a ){
// 0. 定义一个结果变量
long sum = 0L;
// 1. 拆开二维数组
for ( int i = 0 ; i < a.length ; i++ )
{
// 2. 拆开一维数组获取数据
for ( int j = 0 ; j < a[i].length ; j++ )
{
sum+=a[i][j];
}
}
return sum;
}
// 统计二维数组中元素的个数
public static int getDataCount( int [][] a ){
// 0. 记录元素个数
int count = 0;
// 1. 拆开二维数组
for ( int i = 0 ; i < a.length ; i++ )
{
// 2. 拆开一维数组获取数据
for ( int j = 0 ; j < a[i].length ; j++ )
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
int [][] a = new int[][]{ {23,4,5},{2},{4,5,78,56,90} };
printArr2( a );
System.out.println();
System.out.println("累加和是: "+getSum( a ) );
System.out.println("统计元素个数: "+getDataCount( a ) );
System.out.println("Hello World!");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有