Java BASICS (10) and Java BASICS (

Source: Internet
Author: User
Tags testlink

Java BASICS (10) and Java BASICS (
I. Data Structure 1. Definition of Data Structure

Data structures are stored by computers and organized by data. A Data Structure refers to a set of data elements with one or more specific relationships between each other. Generally, a well-selected data structure can improve the running or storage efficiency. Data structures are often related to efficient search algorithms and indexing technologies. (From Baidu encyclopedia)

2. Data Storage Method 1. Variables

Variable declaration format: data type variable name

Example: int age

Variable Value assignment: int age = 22;

Note:

  • Pay attention to the matching of values and types.
  • You must assign a value before using a local variable.
  • A member variable has a default value, but it does not make much sense. assign a value to it when you create a project.

Features of variables:Only one value can be stored..

2. Array

Array declaration format: type of elements in the array [] array name

Example: int [] ages = new int [3];

Array features:Fixed Length. Only data of the same type can be stored..

3. Custom class Storage

CustomizeIt can automatically increase and store data structures of any number of int elements.

Steps:

1. Define a container class intArray.

2. First, you can store multiple int values and use an int array internally.

3. Create an int array in the class. When the int array is full, create a new array, and copy the values in the old array to the new array (automatic resizing ).

Code implementation:

1 import java. util. arrays; 2 3/** 4 * Custom automatically scalable array 5 */6 public class IntArray {7 private int [] data; 8 private int index = 0; 9 10 // constructor, initialization size: 211 public IntArray () {12 this. data = new int [2]; 13} 14 15/** 16 * add data, function 18*19 * @ param i20 */21 public void add (int I) {22 if (index> = data. length) {23 // If the index is greater than the array length, you need to create an array 24 int [] dataNew = new int [data. length + 2]; 25 System. arraycopy (data, 0, dataNew, 0, data. length); // copy data 26 data = dataNew; 27 data [index] = I; // storage value 28 index + = 1; 29} else {30 // If the index is smaller than the array length, the value 31 data [index] = I; 32 index + = 1 is saved directly; 33} 34} 35 36/** 37 * delete data through indexes 38*39 * @ param indexId40 */41 public void del (int indexId) {42 int length = data. length; 43 System. arraycopy (data, indexId + 1, data, indexId, length-indexId-1 ); 44} 45 46/** 47 * search for element 48*49 * @ param indexId50 */51 public int select (int indexId) {52 return data [indexId]; 53} 54 55/** 56 * modify data through indexes 57*58 * @ param indexId59 * @ param value60 */61 public void update (int indexId, int value) {62 data [indexId] = value; 63} 64 65/** 66 * returns the current array length 67*68 * @ return69 */70 public int getLength () {71 return data. length; 72} 73 74/** 75 * search for the first index of the same element 76*77 * @ param value78 * @ return79 */80 public Integer queryByValue (int value) {81 Integer result = null; 82 for (int I = 0; I <data. length; I ++) {83 if (data [I] = value) {84 result = I; 85} 86} 87 return result; 88} 89 90 // override toString method 91 @ Override92 public String toString () {93 // TODO Auto-generated method stub94 // set the value of the element array, copy to the new array 95 int [] newArr = new int [index]; 96 System. arraycopy (data, 0, newArr, 0, index); 97 return Arrays. toString (newArr); 98} 99}

Test the custom expansion array:

1 public class TestIntArray {2/** 3 * Test the custom expansion Array 4 */5 @ Test 6 public void test2 () {7 IntArray intArray = new IntArray (); 8 9 intArray. add (11); 10 intArray. add (22); 11 intArray. add (33); 12 intArray. add (44); 13 intArray. add (55); 14 // Delete 15 intArray Based on the index. del (2); 16 int select = intArray. select (1); 17 System. out. println (select); 18 // update 19 intArray. update (1, 2333); 20 int select1 = intArray. select (1); 21 System. out. println (select1); 22 // query the first index by value 23 Integer index = intArray. queryByValue (55); 24 System. out. println ("index:" + index); 25 // returns the array length 26 int length = intArray. getLength (); 27 System. out. println ("length:" + length); 28 System. out. println (intArray); 29} 30}
2. Linked List 1. linked list definition

A linked list is a non-sequential storage structure on a physical storage unit. The logical sequence of data elements is achieved through the pointer links in the linked list. A linked list consists of a series of nodes (each element in a linked list is called a node), which can be dynamically generated at runtime.Each node consists of two parts: one is the data domain that stores data elements, and the other is the pointer domain that stores the next node address.Compared with the sequential structure of a linear table, the operation is complex. Because it does not have to be stored in order, the chain table can reach O (1) complexity during insertion, which is much faster than the sequential table of another linear table, however, it takes O (n) Time to search for a node or access a node with a specific number. the time complexity of linear and sequential tables is O (logn) and O (1) respectively ).
The linked list structure can be used to overcome the disadvantages that the array linked list needs to know the data size in advance. The linked list structure can make full use of the computer memory space for flexible dynamic memory management. However, the linked list loses the advantage of random array reading. At the same time, the linked list has a large space overhead due to the addition of pointer fields of nodes. The most obvious advantage of a linked list is that the regular array arrangement of associated projects may be different from the order of these data items in the memory or disk. Data Access is usually converted in different order. The linked list allows you to insert and remove nodes at any position on the table, but does not allow random access. There are many different types of linked lists: one-way linked list, two-way linked list, and cyclic linked list. (From Baidu encyclopedia)

2. Implement linked list based on Categories 1. Implementation steps

1. Define the container class.

2. Define Node (pocket class). Two fields are defined in the class. One Object type stores the value, and the other Node type stores the next value.

3. There is a field in the container. In the container class, you should also provide adding, searching, and deleting methods.

Code for the custom Node class:

1/** 2 * Custom Node class 3 */4 public class Node {5 // storage value 6 private Object value; 7 // storage value 8 private Node next; 9 10 public Node () {11 12} 13 14 public Node (Object value) {15 this. value = value; 16} 17 18 public Object getValue () {19 return value; 20} 21 22 public void setValue (Object value) {23 this. value = value; 24} 25 26 public Node getNext () {27 return next; 28} 29 30 public void setNext (Node next) {31 this. next = next; 32} 33}

Code for implementing the linked list:

1/** 2 * implement linked list 3 */4 public class LinkList {5 private Node first; 6 private int size; // linked list size, default Value: 0 7 8/** 9 * method for adding nodes 10*11 * @ param obj12 */13 public void add (Object obj) {14 // 1. Package the data imported by the user. 15 Node node = new Node (obj ); 16 17 // 2. Put the packaged data on first 18 if (first = null) {19 // if it is added for the first time, directly on first 20 first = node; 21} else {22 Node temp = first; 23 // not the first addition. Find the last Node and hold it on the top 24 while (temp. getNext ()! = Null) {25 temp = temp. getNext (); 26} 27 temp. setNext (node); 28} 29 size ++; 30} 31 32 public Node getFirst () {33 return first; 34} 35 36 public void setFirst (Node first) {37 this. first = first; 38} 39 40 public int getSize () {41 return size; 42} 43 44 public void setSize (int size) {45 this. size = size; 46} 47 48 // override toString method 49 @ Override50 public String toString () {51 StringBuffer stringBuffer = new Str IngBuffer (); 52 stringBuffer. append ("["); 53 Node temp = first; // use a temporary variable to store 54 if (temp! = Null) {55 // 56 if (temp. getNext () = null) {57 // No next node 58 stringBuffer. append (temp. getValue (); 59} else {60 stringBuffer. append (temp. getValue ()). append (","); 61} 62} 63 64 while (temp. getNext ()! = Null) {65 // move the pointer down to a position 66 temp = temp. getNext (); 67 if (temp. getNext () = null) {68 // The Last node 69 stringBuffer. append (temp. getValue (); 70} else {71 stringBuffer. append (temp. getValue ()). append (","); 72} 73} 74 stringBuffer. append ("]"); 75 return stringBuffer. toString (); 76} 77}

Test linked list:

1 import org. junit. test; 2 3 public class TestLink {4 5/** 6 * Test linked list 7 */8 @ Test 9 public void testLink () {10 LinkList linkList = new LinkList (); 11 12 linkList. add (11); 13 linkList. add (22); 14 linkList. add (33); 15 linkList. add (44); 16 linkList. add (55); 17 linkList. add (66); 18 int size = linkList. getSize (); 19 System. out. println ("size:" + size); // size: 620 System. out. println (linkList); // [11,22, 33,44, 55,66] 21} 22}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.