Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 6: Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays String , StringBuffer, and StringTokenizer Command-Line Parameters Introducing Arrays • Array is a data structure that represents a collection of the same types of data. • Java treats these arrays as objects. • An Array of 10 Elements of type double double[] myList = new double[10] myList[0] myList[1] myList[2] myList[3] myList[4] myList[5] myList[6] myList[7] myList[8] myList[9] Declaring Arrays datatype[] arrayname; Example: int[] myList; datatype arrayname[]; Example: int myList[]; Creating Arrays arrayName = new datatype[arraySize]; Example: myList = new double[10]; Declaring and Creating in One Step datatype[] arrayname = new datatype[arraySize]; double[] myList = new double[10]; datatype arrayname[] = new datatype[arraySize]; double myList[] = new double[10]; Initializing Arrays Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = (double)i; Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; Example 6.2 Using Arrays in Sorting Objective: Use the selectionSort() method to write a program that will sort a list of double floating-point numbers. TestSelectionSort Run Example 6.3 Testing Linear Search Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then displaying this array. Prompt the user to enter a key for testing the linear search. TestLinearSearch Run Array of Objects Declaring and creating: Circle[] circleArray = new Circle[10]; Initializing: for (int i=0; i<circleArray.length; i++) { circleArray[i] = new Circle(); } Copying Arrays Using a loop: int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i]; The arraycopy Utility arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); Multidimensional Arrays int[][] matrix = new int[10][10]; or int matrix[][] = new int[10][10]; for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { matrix[i][j] = (int)(Math.random()*1000); } Example 6.7 Testing Multidimensional Arrays Objective: Use two-dimensional arrays to create two matrices, and then add the two matrices. TestMatrixAddition Run The String Class Declaring a String: – String message = "Welcome to Java!" – String s = new String(); String Comparisons String Concatenation Substrings String Length Retrieving Individual Characters in a String String Comparisons equals String s1 = "Welcome"; String s2 = "welcome"; if (s1.equals(s2)) { ... } Substrings String is an immutable class; its values cannot be changed individually. String s1 = "Welcome to Java"; String s2 = s1.substring(0,10) + "HTML"; Finding String Length Finding string length using the length() method: message = "Welcome"; message.length() (returns 7) Retrieving Individual Characters in a String Do not use message[0] Use message.charAt(index) Index starts from 0 The StringBuffer Class The StringBuffer class is an alternative to the String class. In general, a string buffer can be used wherever a string is used. StringBuffer is more flexible than String. You can add, insert, or append new contents into a string buffer. However, the value of a string is fixed once the string is created. StringBuffer Constructors public StringBuffer() No characters, initial capacity 16 characters. public StringBuffer(int length) No characters, initial capacity specified by the length argument. public StringBuffer(String str) Represents the same sequence of characters as the string argument. Initial capacity 16 plus the length of the string argument. Appending New Contents into a String Buffer StringBuffer strBuf = new StringBuffer(); strBuf.append("Welcome"); strBuf.append(' '); strBuf.append("to"); strBuf.append(' '); strBuf.append("Java"); The StringTokenizer Class Constructors StringTokenizer(String s, String delim, boolean returnTokens) StringTokenizer(String s, String delim) StringTokenizer(String s) The StringTokenizer Class Methods boolean hasMoreTokens() String nextToken() String nextToken(String delim) Example 6.9 Testing StringTokenizer Objective: Using a string tokenizer, retrieve words from a string and display them on the console. TestStringTokenizer Run Command-Line Parameters class TestMain { public static void main(String[] args) { ... } } java TestMain arg0, arg1, arg2, ..., argn Processing Command-Line Parameters In the main method, get the arguments from args[0], args[1], ..., args[n], which corresponds to arg0, arg1, ..., argn in the command line. Example 6.10 Using Command-Line Parameters Objective: Write a program that will perform binary operations on integers. The program receives three parameters: an operatorTestCommandParameters and two integers. Run Java TestCommandParameters + 2 3 Run Java TestCommandParameters - 2 3 Run Java TestCommandParameters / 2 3