Array, all understand, look directly at the code bar, to achieve the following functions:
Create an array
Find the value on the index
Find if the array contains values
Delete the value on the index
Add a value
Find the position of a value in an array
public class arraystructures {private int[] thearray = new int[50]; Private int arraysize = 10;public void generaterandomarray () {for (int i =0; i< arraysize;i++) {thearray[i] = (int) (Math.random () *10 + 10);}} Public void printarray () {Stringbuffer sb = new stringbuffer ("-");for (int i = 0; i<arraysize; i++) {sb.append ("-----");} String septalline= sb.tostring (); System.out.println (Septalline);for (int i = 0; i<arraysize; i++) { System.out.print ("| " + i + " ");} System.out.println ("|"); System.out.println (Septalline);for (int i = 0; i<arraysize; i++) { System.out.print ("| " + theArray[i] + " ");} System.out.println ("|"); System.out.println (septalline);} Public int getvalueatIndex (Int index) {if (index< arraysize) return thearray[index];return 0;} Public boolean doesarraycontainthisvalue (Int value) {for (int i=0; i<arraysize; i++) {if (thearray[i]== value) return true;} Return false;} Public void deletevalueofindex (Int index) {for (int i=index; i<arraysize; i++) { THEARRAY[I]=&NBSP;THEARRAY[I+1];} arraysize--;} Public void insertvalue (Int value) {if (arraysize<50) {thearray[arraysize] = value; arraysize++;}} Public string linearsearchforvalue (Int value) {boolean valueinarray = false; String indexswithvalue = ""; System.out.print ("the value at index: "); for (int i=0; i<arraysize; i++) { if (thearray[i] == value) {if (!valueinarray) {valueinarray = true;} System.out.print (i+ " ");indexswithvalue += i+ " ";}} if (!valueinArray) {indexswithvalue = "None"; System.out.println (Indexswithvalue);} Return indexswithvalue;} Public static void main (String[] args) {// Generate an ArraySystem.out.println ("Create an array, and fill in random value"); Arraystructures as = new arraystructures ();// set random value in array as.generaterandomarray ();// print original array as.printarray (); System.out.println (); System.out.println ("value at index 3"); System.out.println (As.getvalueatindex (3)); System.out.println (); System.out.println ("does this array contain value 14"); System.out.println (As.doesarraycontainthisvalue (14)); System.out.println (); System.out.println ("delete value at index 4"); As.deletevalueofindex (4); As.printarray (); System.out.println (); System.out.println ("Add value 33 at the end&nbsP;of array "); As.insertvalue (); As.printarray (); System.out.println (); System.out.println ("search posittions which value is 12"); Linearsearchforvalue (12); System.out.println ();}}
Output Results
create an array, and fill in random Value---------------------------------------------------| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |---------------------------------------------------| 19 | 12 | 13 | 12 | 18 | 12 | 11 | 16 | 18 | 10 |---------------------------------------------------Value at index 312does this array contain value 14falsedelete value at index 4----------------------------------------------| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |----------------------------------------------| 19 | 12 | 13 | 12 | 12 | 11 | 16 | 18 | 10 |----------------------------------------------add value 33 at the end of array---------------------------------------------------| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |------------------------------------------- --------| 19 | 12 | 13 | 12 | 12 | 11 | 16 | 18 | 10 | 33 |---------------------------------------------------Search posittions which value is 12the value at index: 1 3 4
This article is from the "10314466" blog, please be sure to keep this source http://10324466.blog.51cto.com/10314466/1658705
Data structures and algorithms-001 arrays