In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The first data structure we want to learn is the array, many of which are worth mining.
Array basis
Code the data into a row for storage
The index in the array starts at 0, and the Java syntax requires the array to store elements of the same type, which can be fetched by subscript in parentheses.
So you can see some of the methods in Main.
Package cn.mtianyan
Public class Main {
Public static void main (String [] args) {/ / must pass the length int [] arr = new int [10]; for (int I = 0; I)
< arr.length; i++) { arr[i] = i; } System.out.println("========="); int[] scores = new int[]{100,99,96}; for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); } System.out.println("========="); for (int score : scores) { System.out.println(score); } System.out.println("========="); scores[0] = 92; for (int score : scores) { System.out.println(score); }} } 数组可以这样遍历,是因为它实现了可遍历接口。 Java为我们提供的数组,其中非常重要的一部分就是它的索引。索引可以有语义,也可以没有语义,2可以代表学号,但也可以将其视为没有语义。 数组最大的优点: 快速查询,scores[2]直接插到2号索引的。因此数组最好应用于"索引有语义"的情况,查学号为2的学生成绩。 但并非所有有语意的索弓都适用于数组,如×××号: 1101031985121 66666;依次为索引,这得开辟很大的内存空间,空间浪费。 数组也可以处理"索引没有语意"的情况。我们在这一章,主要处理"索引没有语意"的情况数组的使用。 索引没有语意,如何表示没有元素? capacity 和 size的区别: 数组size是3,capacity是8。 如何添加元素(超过size如何处理)?如何删除元素(挪位)? Java中的数组并没有这些方法,我们需要基于java的数组,二 次封装属于我们自己的数组类。 我们自己做的是动态数组(内部依然使用java数组实现),Java的是静态数组。 增删改查,有的数据结构不一定四个齐全。capacity是数组最多可以装多少元素,和实际能装多少元素是没有关系的。 package cn.mtianyan; public class Array { private int[] data; private int size; /** * 带容量参数构造函数 * * @param capacity 数组容量 */public Array(int capacity) { data = new int[capacity]; size = 0;}/** * 默认构造函数 */public Array() { this(10);}/** * 静态数组入参构造函数 * * @param data 传入静态数组 */public Array(int[] data) { this.data = data;}/** * 获取数组元素个数 * * @return size 数组元素个数 */public int getSize() { return size;}/** * 获取数组的容量 * * @return capacity 获取容量 */public int getCapacity(){ return data.length;}/** * 判断数组是否为空 * * @return 是否为空 */public boolean isEmpty(){ return size == 0;} } 向数组中添加元素 向数组末尾添加元素 size指向data中第一个为空位置。因此添加元素时只需要添加到arr[size],并size++即可。(注意添加元素判满) /** * 向所有元素末尾添加一个新元素。 * * @param e 添加的元素 */public void addLast(int e){ // if (isFull()){ // throw new IllegalArgumentException("AddLast failed. Array is Full"); // }else { // data[size] =e; // data[size++] =e; // size++; // } add(size,e); } /** * 向索引0号位置添加元素 * * @param e 添加的元素 */public void addFirst(int e){ add(0,e);}/** * 在index位置插入一个新元素e * * @param index 插入位置索引 * @param e 插入元素 */public void add(int index,int e){ if (isFull()) throw new IllegalArgumentException("AddLast failed. Array is Full"); // 大于size就不是紧密排列了 if (indexsize){ throw new IllegalArgumentException("AddLast failed. Required indexsize "); } else { // 从哪开始挪呢: 从size-1这个元素(size本身是指向空的),挪到哪个呢,index位置的这个元素也是要挪的。 for (int i=size-1; i>= index; iMurt -) {data [iTune1] = data [I]; / / the last one equals the previous one, starting with the last element of the array. / / extreme validations: size 1 index 0; data [1] = data [0] will be executed once. That's right. } data [index] = e; size++;}}
77 insert, the following elements should be moved backward.
Note here that you must move back from 100 here, otherwise it will cause a value overwrite.
Query and modify elements in an array
/ * prints array information and traverses elements. * * @ return array information and element traversal results * / @ Overridepublic String toString () {StringBuilder res = new StringBuilder (); res.append ("Array: size =% d, capacity =% d\ n", size,data.length)); res.append ('['); for (int I = 0; I)
< size; i++) { res.append(data[i]); if (i !=size-1) // 最后一个元素不要加, res.append(", "); } res.append(']'); return res.toString();} package cn.mtianyan; public class ArrayTest { public static void main(String[] args) { Array array = new Array(20); for (int i = 0; i < 10; i++) { array.addLast(i); } System.out.println(array); } } array.add(1,100); System.out.println(array); array.addFirst(-1); System.out.println(array); 取出某一个元素。 /** * 传入索引,获取该位置元素 * * @param index 要获取的元素索引 * @return 返回该位置数组元素 */public int get(int index){ if (index=size){ throw new IllegalArgumentException("Get failed. Required index=size "); }else { return data[index]; }} 通过get方法用户无法使用到数组后半截没有使用到的空间,通过封装保证。 System.out.println(array.get(array.getSize()-1)); /** * 传入索引和元素值,将该位置元素设置为传入值 * @param index 索引 * @param e 传入值 */public void set(int index,int e){ if (index=size){ throw new IllegalArgumentException("Set failed. Required index=size "); }else { data[index] = e; }} array.set(0,-99); System.out.println(array); 数组中的包含,搜索和删除元素 如果要删除索引为1的元素,从88开始,后面都要往前挪,挪size-index次。只需要size--,对于最后一个位置的元素不用做其他操作,因为用户也访问不到。 /** 查找数组中是否有元素e@param e @return 包含 true; 不包含 false */ public boolean contains(int e){ for (int i = 0; i < size; i++) { if (data[i] == e){ return true; } } return false; } /** 查找数组中元素,返回其索引(第一个遇到)@param e 元素 @return 不存在 -1; 存在 i */ public int find(int e){ for (int i = 0; i < size; i++) { if (data[i] == e){ return i; } } return -1; } public int[] findAll(int e){ int[] tempArray=new int[size]; int index = 0; for (int i = 0; i < size; i++) { if (data[i] == e){ tempArray[index] = i; index++; } } int[] indexArray=new int[index]; for (int i = 0; i < index; i++) { indexArray[i] = tempArray[i]; } return indexArray; } 注意findAll中使用tempArray临时对象的作用: 同时将int数组,与它的size一起传递了出来。 System.out.println("===============加入重复元素后数组如下===================");array.add(3,3);array.add(array.getSize()-1,9);System.out.println(array);System.out.println("================包含 寻找测试===========================");System.out.println(array.contains(-99));System.out.println(array.contains(-100));System.out.println("3的index: "+array.find(3));int [] tmpArr = array.findAll(3);for (int i : tmpArr) { System.out.print(i+" ");}System.out.println(); /** 删除数组元素,返回删除的元素值@param index 索引 @return 该索引位置元素值 */ public int remove(int index){ // 判空,空数组 removeFirst index=0,size=0,index>= size exception. Empty array removeLast index=-1 index=size exception. Empty array removeLast index=-1 index=size exception. Empty array removeLast index=-1 index
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.