Day12 顺序表(二)

2022/4/29 6:13:01

本文主要是介绍Day12 顺序表(二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

  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

 



这篇关于Day12 顺序表(二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程