ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Day12 顺序表(二)

2022-04-29 02:32:42  阅读:118  来源: 互联网

标签:顺序 tempPosition int length Day12 tempFirstList data out


就是顺序表的增删改查,挺有意思的

  1 public class SequentialList {
  2 
  3     /**
  4      * The maximal length of the list. It is a constant.
  5      */
  6     public static final int MAX_LENGTH = 10;
  7 
  8     /**
  9      * The actual length not exceeding MAX_LENGTH.
 10      */
 11     int length;
 12 
 13     /**
 14      * The data stored in an array.
 15      */
 16     int[] data;
 17 
 18     /**
 19      * Construct an empty sequential list.
 20      */
 21     public SequentialList() {
 22         length = 0;
 23         data = new int[MAX_LENGTH];
 24     }
 25 
 26     /**
 27      * Construct a sequential list using an array.
 28      * 
 29      * @param paraArray
 30      */
 31     public SequentialList(int[] paraArray) {
 32         data = new int[MAX_LENGTH];
 33         length = paraArray.length;
 34 
 35         // Copy data.
 36         for (int i = 0; i < paraArray.length; i++) {
 37             data[i] = paraArray[i];
 38         } // Of for i
 39     }// Of for the second constructor
 40 
 41     /**
 42      * Overrides the method claimed in Object, the superclas of any class.
 43      */
 44     public String toString() {
 45         String resultString = "";
 46 
 47         if (length == 0) {
 48             return "empty";
 49         }
 50 
 51         for (int i = 0; i < length - 1; i++) {
 52             resultString += data[i] + ", ";
 53         } // Of for i
 54 
 55         // the last member of resultString
 56         resultString += data[length - 1];
 57 
 58         return resultString;
 59     }// Of for toString
 60 
 61     public void reset() {
 62         length = 0;
 63     }// Of reset
 64 
 65     /**
 66      * The entrance of the program.
 67      * 
 68      * @param args
 69      */
 70     public static void main(String args[]) {
 71         int[] tempArray = { 1, 4, 6, 9 };
 72         SequentialList tempFirstList = new SequentialList(tempArray);
 73         System.out.println("After initialized, the list is: " + tempFirstList.toString());
 74         System.out.println("Again, the list is: " + tempFirstList);
 75 
 76         int tempValue = 4;
 77         int tempPosition = tempFirstList.indexOf(tempValue);
 78         System.out.println("The position of " + tempValue + " is " + tempPosition);
 79     
 80         tempValue = 5;
 81         tempPosition = tempFirstList.indexOf(tempValue);
 82         System.out.println("The position of " + tempValue + " is " + tempPosition);
 83 
 84         tempValue = 5;
 85         tempPosition = 2;
 86         tempFirstList.insert(tempPosition, tempValue);
 87         System.out.println("After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);
 88 
 89         tempValue = 10;
 90         tempPosition = 8;
 91         tempFirstList.insert(tempPosition, tempValue);
 92         System.out.println("After inserting " + tempValue + " to position " + tempPosition + ", the list is: " + tempFirstList);
 93 
 94         tempPosition = 3;
 95         tempFirstList.delete(tempPosition);
 96         System.out.println("After deleting data at position " + tempPosition + ", the list is: " + tempFirstList);
 97 
 98         for (int i = 0; i < 8; i++) {
 99             tempFirstList.insert(i, i);
100             System.out.println("After inserting " + i + " to position " + i + ", the list is: " + tempFirstList);
101         }// Of for i
102 
103         tempFirstList.reset();
104         System.out.println("After reset, the list is: " + tempFirstList);
105     }// Of main
106 
107     /**
108      * Find the index of the given value.
109      * If it appears in multiple positions,simply return the first one.
110      * 
111      * @param paraValue
112      * @return
113      */
114     public int indexOf(int paraValue) {
115         int tempPosition = -1;
116         for (int i = 0; i < length; i++) {
117             if (data[i] == paraValue) {
118                 tempPosition = i;
119                 break;
120             } // Of if
121         } // Of for i
122 
123         return tempPosition;
124     }// Of indexOf
125 
126     /**
127      * Insert a value to a position.
128      * If the list is already full, do nothing.
129      * 
130      * @param paraPosition
131      * @param paraValue
132      * @return
133      */
134     public boolean insert(int paraPosition, int paraValue) {
135         if (length == MAX_LENGTH) {
136             System.out.println("List full.");
137             return false;
138         } // Of if
139 
140         if ((paraPosition < 0) || (paraPosition > length)) {
141             System.out.println("The position " + paraPosition + " is out of bounds.");
142             return false;
143         } // Of if
144 
145         // From tail to head, the last one is moved to a new position.
146         // Because length < MAX_LENGTH, no exceeding occurs.
147         for (int i = length; i > paraPosition; i--) {
148             data[i] = data[i - 1];
149         } // Of for i
150 
151         data[paraPosition] = paraValue;
152         length++;
153 
154         return true;
155     }// Of insert
156 
157     /**
158      * Delete a value at a position.
159      * 
160      * @param paraPosition
161      * @return
162      */
163     public boolean delete(int paraPosition) {
164         if ((paraPosition < 0) || (paraPosition > length)) {
165             System.out.println();
166             return false;
167         } // Of if
168 
169         // From head to tail.
170         for (int i = paraPosition; i < length - 1; i++) {
171             data[i] = data[i + 1];
172         }// Of for i
173 
174         length--;
175 
176         return true;
177     }// Of delete
178 
179 }// Of class SequentialList

 

标签:顺序,tempPosition,int,length,Day12,tempFirstList,data,out
来源: https://www.cnblogs.com/f1uency/p/16205221.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有