Day6
public static void main(String[] args) {
int[] nums;//定义一个数组
nums=new int[10];
nums[0]=1;
nums[1]=2;
nums[2]=3;
nums[3]=4;
nums[4]=5;
nums[5]=6;
nums[6]=7;
nums[7]=8;
nums[8]=9;
nums[9]=10;
// System.out.println(nums[9]);
//计算所有元素之和
int sum=0;
//获取数组长度: arrays.length
for (int i = 0; i < nums.length; i++) {
sum=sum+nums[i];
}
System.out.println(sum);
}
public static void main(String[] args) {
int[] array=new int[10];
array[0]=1;
array[1]=2;
array[2]=3;
array[3]=4;
array[4]=5;
array[5]=6;
array[6]=7;
array[7]=8;
array[8]=9;
array[9]=10;
for (int i=1;i<array.length;i++){
System.out.println(array[i]);
}
}
public static void main(String[] args) {
int[] array={1, 2, 3, 4, 5};
for (int i=0;i<array.length;i++){
System.out.println(array[i]);
}
System.out.println("=======================================");
//所有元素之和
int sum=0;
for (int i=0;i< array.length;i++){
sum+=array[i];
}
System.out.println("sum="+sum);
System.out.println("=====================================================");
//最大值
int max=0;
for (int i=0;i<array.length;i++){
max=array[i];
}
System.out.println("max="+max);
}
public static void main(String[] args) {
int[] arrays={1,2,3,4,5};
int[] reverse=reverse(arrays);
pritArray(reverse);
}
//反转数组
public static int[] reverse(int[] arrays){
int[] result =new int[ arrays.length];
//操作
for (int i = 0,j=result.length-1; i <arrays.length ; i++,j--) {
result[j]=arrays[i];
}
return result;
}
public static void pritArray(int[] arrays){
for (int i = 0; i < arrays.length ; i++) {
System.out.println(arrays[i]+"");
}
}
public static void main(String[] args) {
//[4][2]
/*
1,2 array[0]
2,3 array[1]
3,4 array[2]
4,5 array[3]
*/
int[][] array={{1,2},{2,3},{3,4},{4,5}};
for (int i = 0; i < array.length ; i++) {
for (int j = 0; j <array[i].length ; j++) {
System.out.println(array[i][j]);
}
}
}
public static void main(String[] args) {
int[] a={1,2,3,4,9999,31231,543,21,3,23};
System.out.println(a);
//打印数组元素Array.toStringg
System.out.println(Arrays.toString(a));
printArray(a);
}
public static void printArray(int[]a){
for (int i = 0; i <a.length ; i++) {
if (i==0) {
System.out.print("[");
}
if (i==a.length-1) {
System.out.print(a[i] + "]");
}else {
System.out.print(a[i]+",");
}
}
}
public static void main(String[] args) {
int[] a={1,2,3,4,99,777,444,3,3,8,7,6,1000};
int[] sort= sort(a);
System.out.println(Arrays.toString(sort));
}
public static int[] sort(int[]array){
int temp=0;
//
for (int i = 0; i < array.length-1; i++) {
//
for (int j =0; j < array.length-1-i; j++) {
if (array[j+1] > array[j]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
public static void main(String[] args) {
int[][] deter=new int[11][11];
deter[1][2]=1;
deter[2][10]=2;
System.out.println("输出");
for (int[] bai:deter){
for (int hei:bai){
System.out.print(hei+"\t");
}
System.out.println();
}
}
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(。。。){
//方法体
return 返回值;
}
*/
public String sayHello(){
return "hello world";
}
public void print(){
return;
}
public int max(int a,int b){
return a>b? a:b; //三元运算符
}
标签:Jjava,--,void,nums,int,初识,static,array,public
From: https://www.cnblogs.com/determine-boke/p/17993269