前言 Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、 游戏 控制台、科学超级计...
本文标题java怎样新建一个数组(java声明数组的写法),作者:知世,本文有695个文字,大小约为3KB,预计阅读时间2分钟,请您欣赏。知世金融网众多优秀文章,如果想要浏览更多相关文章,请使用网站导航的搜索进行搜索。本站虽然不乏优秀之作,但仅作为投资者学习参考。
Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
首先必须声明数组变量,才能在程序中使用数组。下面是声明数组变量的语法:
dataType[] arrayRefVar; //首选的方法//或dataType arrayRefVar[]; //效果相同,但不是首选方法
Java语言使用new操作符来创建数组,语法如下:
dataType[] arrayRefVar = new dataType[arraysize];
数组的元素是通过索引访问的,数组索引从0开始。
获取数组长度: arrays.length
静态初始化:
int[] a = {1,2,3};Man[] mans = {new Man(1,1), new Man(2,2)};
动态初始化:
int [] a = new int[2];a[0] = 1;a[1] = 2;
数组的默认初始化:
数组的四个基本特点:
数组边界:
小结:
int[] arrays = {1,2,3,4,5};for(int array : arrays){ System.out.println(array);}
数组作方法入参
public class Array { public static void main(String[] args) { int[] arrays = {1,2,3,4,5}; printArray(arrays); } //打印数组元素 public static void printArray(int[] arrays){ for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i] + " "); } }}
数组作返回值
public class Array { public static void main(String[] args) { int[] arrays = {1,2,3,4,5};// printArray(arrays); int[] result = reverse(arrays); printArray(result); } //反转数组 public static int[] reverse(int[] arrays){ int[] result = new int[arrays.length]; for( int i = 0, j = arrays.length-1 ; i< arrays.length ; i++ , j--){ result[j] = arrays[i]; } return result; } //打印数组元素 public static void printArray(int[] arrays){ for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i] + " "); } }}
int[][] a = new int[2][5];
解析:以上二维数组a可以看成一个两行五列的数组。
public class erweiArray { public static void main(String[] args) { int[][] arrays = {{1,2},{2,3},{3,4},{4,5}}; //遍历二维数组 for (int i = 0; i < arrays.length; i++) { for (int j = 0; j < arrays[i].length; j++) { System.out.println(arrays[i][j]); } } }}
具有以下常用功能:
import java.util.Arrays;public class ArraysLei { public static void main(String[] args) { int[] a = {1,2,3,4,12345,43,12342,98,1243}; Arrays.sort(a); //数组排序:升序 System.out.println(Arrays.toString(a)); //打印数组元素 Arrays.fill(a,2,4,0); //数组填充 System.out.println(Arrays.toString(a)); }}
冒泡的代码还是相当简单的,两层循环,外层冒泡轮数,里层依次比较,江湖中人人尽皆知。我们看到嵌套循环,应该立马就可以得出这个算法的 时间复杂度为O(n2) 。
import java.util.Arrays;//冒泡排序://1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置;//2.每一次比较,都会产生出一个最大,或者最小的数字;//3. 下一轮则可以少一次排序!//4. 依次循环,直到结束!public class maopaopaixu { public static void main(String[] args) { int[] a = {1,4,5,72,2,2,2,25,6,7}; 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; }}
1 public class Student 2 { 3 // 成员变量 4 private String name; 5 private int age; 6 7 // 构造方法 8 public Student() 9 {10 super();11 }12 13 public Student(String name, int age) 14 {15 super();16 this.name = name;17 this.age = age;18 }19 20 // 成员方法21 // getXxx()/setXxx()22 public String getName() 23 {24 return name;25 }26 27 public void setName(String name) 28 {29 this.name = name;30 }31 32 public int getAge() 33 {34 return age;35 }36 37 public void setAge(int age) 38 {39 this.age = age;40 }41 42 @Override43 public String toString() 44 {45 return "Student [name=" + name + ", age=" + age + "]";46 }47 }
【参考文档】
1 /** 2 把5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。 3 * 学生:Student 4 * 成员变量:name,age 5 * 构造方法:无参,带参 6 * 成员方法:getXxx()/setXxx() 7 * 分析: 8 * A:创建学生类。 9 * B:创建学生数组(对象数组)。10 * C:创建5个学生对象,并赋值。11 * D:把C步骤的元素,放到数组中。12 * E:遍历学生数组。13 * */14 15 public class Practice 16 {17 public static void main(String[] args)18 {19 // 创建学生数组(对象数组)。20 Student[] students = new Student[5];21 // for (int x = 0; x < students.length; x++) 22 // {23 // System.out.println(students[x]);24 // }25 // System.out.println("---------------------");26 27 // 创建5个学生对象,并赋值。28 Student s1 = new Student("小明", 27);29 Student s2 = new Student("小红", 30);30 Student s3 = new Student("小强", 30);31 Student s4 = new Student("旺财", 12);32 Student s5 = new Student("张三", 35);33 34 // 将对象放到数组中。35 students[0] = s1;36 students[1] = s2;37 students[2] = s3;38 students[3] = s4;39 students[4] = s5;40 41 // 遍历42 for (int x = 0; x < students.length; x++) 43 {44 //System.out.println(students[x]);45 Student s = students[x];46 System.out.println(s.getName()+"---"+s.getAge());47 }48 }49 }
集合类的由来:面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。
数组和集合类同的区别:
数组可以存储同一种类型的基本数据也可以存储同一种类型的对象,但长度是固定的
集合只可以存储不同类型的对象,长度是可变的
集合类的特点:集合只用于存储对象,集合长度是可变的,集合可以存储不同类型的对象。
集合容器因为内部的数据结构不同,有多种具体容器,根据共性内容不断的向上抽取,就形成了集合框架。
框架的顶层Collection接口
Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。
本文相关推荐: 重庆“燃气表跑得快”事件:成立含公安的联合调查组进驻燃气企业
以上便是知世金融网给大家分享的关于java怎样新建一个数组(java声明数组的写法)/xwzx/gppz/212808.html的相关信息了,希望能帮助到大家,更多金融相关信息,敬请关注知世金融网!
网站内容均来自互联网,如侵害您的利益联系客服进行删除!
下一篇:免费的pdf编辑器去水印(教你迅捷pdf去水印的方法)
本文标题:java怎样新建一个数组(java声明数组的写法)
本文地址:/index.php?s=article&c=search&keyword=%E5%89%8D%E8%A8%80