4、数组

一.数组

(1)定义数组
语法:数据类型[] 数组名;
数据类型 数组名[];

(2)指定数组大小:
语法:数组名=new 数据类型[size];

(3)给数组赋值
语法:数组名[index]=值;
注意:数组index索引下标从0开始

(4)取出数组的某个值
语法:数组名[index];

(5)数组的属性
语法:数组名.length;
作用:求得数组的长度

二、示例

2.1 示例:简单定义数组并赋值

 1 package com.test;
 2
 3 public class Test {
 4     public static void main(String[] args) {
 5         //1.定义数组
 6         int[] score;
 7
 8         //2.给数组指定大小
 9         score=new int[5];
10
11         //3.给数组赋值
12         score[0]=15;
13         score[1]=26;
14         score[2]=36;
15         score[3]=46;
16
17         //4.取出数组中的值
18         int num=score[1];
19         System.out.println(num);
20
21     }
22
23 }

2.2 示例:循环给数组赋值和取值

 1 package com.test;
 2
 3 import java.util.Scanner;
 4
 5 public class Test1 {
 6
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         Scanner input=new Scanner(System.in);
12         System.out.print("请指定数组的大小:");
13         int size=input.nextInt();
14
15         //1.定义并确定数组大小
16         int[] score=new int[size];
17         //通过数组名.length获取数组的空间大小
18         System.out.println("数组的空间大小:"+score.length);
19
20         //2.循环给数组赋值
21         for (int i = 0; i < score.length; i++) {
22             System.out.print("请输入第:"+(i+1)+"个值");
23             score[i]=input.nextInt();
24             //score[i]=(int) (Math.random()*10);
25             System.out.println("完成第"+(i+1)+"次赋值");
26         }
27
28         //3.循环取值
29         for (int i = 0; i < score.length; i++) {
30             System.out.println("第"+i+"位置上的值为:"+score[i]);
31         }
32
33     }
34
35 }

2.3 示例:用foreach循环打印数组

 1 package com.test;
 2
 3 import java.util.Scanner;
 4
 5
 6 public class Test2 {
 7     public static void main(String[] args) {
 8         Scanner input=new Scanner(System.in);
 9         int num[]=new int[]{8,4,54,36,35,8};
10         int sum=0;
11         //for(数组的数据类型或集合的数据类型  循环增量:数组或集合的名称)
12         for (int i : num) {
13             System.out.println(i);
14         }
15
16         /*System.out.println("请输入一个数字");
17         int shuzi=input.nextInt();*/
18
19         /*for(int i=0;i<num.length;i++){
20             if(num[i]==shuzi){
21                 System.out.println("对应的索引下标:"+i);
22                 System.out.println("数组中包含此数为"+num[i]);
23             }
24             sum+=num[i];
25         }
26         */
27
28
29         System.out.println("数组的和是:"+sum);
30     }
31
32 }

2.4 示例:请数组最大值

 1 package com.test;
 2 /**
 3  * 求最大值
 4  * @author Dell
 5  *
 6  */
 7 public class Test {
 8     public static void main(String[] args) {
 9         int[] score=new int[5];
10         score[0]=23;
11         score[1]=20;
12         score[2]=2;
13         score[3]=30;
14         score[2]=18;
15         int max=score[0];
16         for (int i = 1; i < score.length; i++) {
17             if(score[i]>max){
18                 max=score[i];
19             }
20         }
21         System.out.println("最大值为:"+max);
22     }
23
24 }

2.5 示例:求数组最小值

 1 package com.test;
 2 /**
 3  * 求最小值
 4  * @author Dell
 5  *
 6  */
 7 public class Test1 {
 8     public static void main(String[] args) {
 9         int[] score=new int[5];
10         score[0]=99;
11         score[1]=100;
12         score[2]=82;
13         score[3]=63;
14         score[4]=88;
15
16         int min=score[0];
17         for (int i = 1; i < score.length; i++) {
18             if(min>score[i]){
19                 min=score[i];
20             }
21         }
22         System.out.println("最大值为:"+min);
23     }
24 }

2.6 示例: 3.在原有降序数组中插入值,完后还是降序

 1 package com.test;
 2 /**
 3  * 插入一个值,插入完之后是降序
 4  * @author Dell
 5  *
 6  */
 7 public class Test3 {
 8     public static void main(String[] args) {
 9         int[] score=new int[6];
10         score[0]=99;
11         score[1]=98;
12         score[2]=97;
13         score[3]=96;
14         score[4]=18;
15         int index=0;//定义要插入的所有位置
16         int num=6; //要插入的值
17         //确定要插入的位置
18        for (int i = 0; i < score.length; i++) {
19           if(num>score[i]){
20               index=i;
21               break;
22           }
23        }
24
25        //循环将元素后移
26        for (int j = score.length-1; j >index; j--) {
27           score[j]=score[j-1];
28        }
29
30        //插入元素
31        score[index]=num;
32
33        //插入完成之后
34        for (int k = 0; k < score.length; k++) {
35         System.out.println(score[k]);
36     }
37     }
38
39 }

2.7 示例:冒号排序:从小到大

 1 package com.test;
 2
 3 public class Test4 {
 4
 5     /**
 6      * 冒号排序:从小到大排列
 7      */
 8     public static void main(String[] args) {
 9          int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
10             int temp=0;
11             for(int i=0;i<a.length-1;i++){
12                 for(int j=0;j<a.length-1-i;j++){
13                 if(a[j]>a[j+1]){
14                     temp=a[j];
15                     a[j]=a[j+1];
16                     a[j+1]=temp;
17                 }
18                 }
19             }
20             for(int i=0;i<a.length;i++)
21                 System.out.println(a[i]);
22         }
23
24     }

2.8 冒号排序:从大到小

 1 package com.test;
 2
 3 public class Test5 {
 4
 5     /**
 6      * 冒号排序:从大到小排列
 7      */
 8     public static void main(String[] args) {
 9          int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
10             int temp=0;
11             for(int i=0;i<a.length-1;i++){
12                 for(int j=0;j<a.length-1-i;j++){
13                 if(a[j]<a[j+1]){
14                     temp=a[j];
15                     a[j]=a[j+1];
16                     a[j+1]=temp;
17                 }
18                 }
19             }
20             for(int i=0;i<a.length;i++)
21                 System.out.println(a[i]);
22         }
23
24     }

2.9 示例 :Arrays类的使用

 1 package com.test;
 2
 3 import java.util.Arrays;
 4
 5 /**
 6  * Arrays类的使用
 7  * @author pc
 8  *
 9  */
10 public class Test6 {
11     public static void main(String[] args) {
12         int[] score=new int[5];
13         score[0]=99;
14         score[1]=16;
15         score[2]=97;
16         score[3]=88;
17         score[4]=60;
18         //完成数组升序排序
19         Arrays.sort(score);
20
21         for (int i : score) {
22             System.out.println(i);
23         }
24
25         //将数组变成字符串
26         System.out.println(Arrays.toString(score));
27
28     }
29
30 }

2.10 示例 :二维数组的使用

 1 package com.test;
 2
 3 public class Test7 {
 4
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         int[][] xy=new int[2][3];
10         xy[0][0]=90;
11         xy[0][1]=70;
12         xy[0][2]=50;
13         xy[1][0]=80;
14         xy[1][1]=80;
15         xy[1][2]=40;
16
17     }
18
19 }
时间: 2024-08-07 17:01:27

4、数组的相关文章

移除数组中第一个负数后的所有负数

scala> val a = ArrayBuffer[Int](1, 2,3, 5, -1, 2, -3, -5) a: scala.collection.mutable.ArrayBuffer[Int]= ArrayBuffer(1, 2, 3, 5, -1, 2, -3 , -5)   scala> :paste // Entering paste mode (ctrl-D tofinish)   var foundFirstNegative = false val keepIndexes

NumPy基础:数组和失量计算

NumPy : Numerical Python,是高性能科学计算和数据分析的基础包. 部分功能: ndarray:一个具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组: 用于对整组数据进行快速运算的标准数学函数(无需编写循环): 用于读写磁盘数据的工具以及用于操作内存映射文件的工具: 线性代数.随机数生成以及傅里叶变换功能: 用于集成C.C++.Fortran等语言编写的代码工具: 大部分数据分析应用关注的功能: 用于

Matlab - 求数组的零值与过零点索引

function zeroindex=pickzero(x)%找出数组的零值及过零点(正负相交处,可能偏离0)m = length(x);x1=x(1:m-1);x2=x(2:m);indz = find(x==0); %zero pointindzer = find(x1.*x2<0); %negative/positiven=length(indzer);for i=1:n if abs(x(indzer(i)))>abs(x(indzer(i)+1)) indzer(i)=indzer(

Java中数组的概念

1.什么是二维数组?有几种表达方式?分别是什么? 答:多维数组即数组的数组,即数组的元素也是数组. 例:int[] [] a = {{1},{1,2},{1,2,3}}; 有三种方式 1).int [] [] a;  2).int [] a1 [];  3).int a2 [] []; *强烈推荐用第1种,不容易混淆a的数据类型: 2.多维数组的创建过程是什么? 答: 例:int [] [] a = new int [2] []; a[0] = {1,2,3}; a[1] = {4,5,6};

ES6之主要知识点(六)数组

引自http://es6.ruanyifeng.com/#docs/array 1.扩展运算符(...) 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. 该运算符主要用于函数调用. function push(array, ...items) { array.push(...items); } function add(x, y) { return x + y; } var numbers = [4, 38]; add(...n

数组、字符串、集合

数组与集合的转换.数组与字符串的转换 ========数组变集合 String[] arr = {"abc","cc","kkkk"}; //把数组变成list集合有什么好处? /* 可以使用集合的思想和方法来操作数组中的元素. 注意:将数组变成集合,不可以使用集合的增删方法. 因为数组的长度是固定. contains. get indexOf() subList(); 如果你增删.那么会产生UnsupportedOperationExcepti

c#数组的count()和length的区别

C# 数组中 Length 表示数组项的个数,是个属性. 而 Count() 也是表示项的个数,是个方法,它的值和 Length 一样.但实际上严格地说 Count() 不是数组的内容,而是 IEnumerable 的内容.这也是为什么 C# 2.0 时数组不能用 Count(),而 3.0 后就可以用 Count() 的原因. 对于数组,据说用 Length 快于 Count(). 所以一般情况:数组我用 Length,IEnumerable(比如 List)我用 Count().

Falsy Bouncer(过滤数组假值)

Falsy Bouncer 过滤数组假值 (真假美猴王) 删除数组中的所有假值. 在JavaScript中,假值有false.null.0."".undefined 和 NaN. function bouncer(arr) { // 请把你的代码写在这里 return arr.filter(function(a){ return !!a; }); } bouncer([false, null, 0, NaN, undefined, ""]); 本来也不会,参考了别人

最大连续子数组,线性时间解法

思想: 经过分析可得,若子数组和为负数就已经代表这个子数组不可能为最大子数组了,相反若子数组和为正,则将最大的和比较出来便可. 故可直接遍历该数组一旦子数组和已为负数,则置为0,否则与之前的最大值进行比较,得出目前最大值. 上代码: #include<iostream> using namespace std; int getMax(int *arr,int n,int start,int end){ int max; int firstmax = arr[0]; max = arr[0];

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include