Java SE 02 Code Study Notes

2010-05-08  来源:本站原创  分类:Java  人气:178 

02 code with study notes

Array of related API

/**
 *  The test class Arrays
 */
package org.sean.module02;

import java.util.Arrays;

/**
 * @author  Sun  (Sean Yang)
 */
public class TestArrays {

        /**
         *  Integer array output
         *
         * @param array
         */
        public static void iterator(int[] array) {
                if (array != null) {
                        for (int i = 0; i < array.length; i++) {
                                System.out.print(array[i] + " ");
                        }
                }
                System.out.println();
        }

        public static void main(String[] args) {
                /**  Fill the entire array   */
                int[] array0 = new int[10];
                Arrays.fill(array0, 5);
                iterator(array0);
                /**  Fills the array at the specified location   */
                Arrays.fill(array0, 3, 8, 10);
                iterator(array0);

                /**  An array of sort   */
                int[] array1 = { 7, 8, 4, 5, 1, 12, 9 };
                System.out.print(" Before you sort  ");
                iterator(array1);
                Arrays.sort(array1);
                System.out.print(" After sorting  ");
                iterator(array1);

                /**  The contents of an array   */
                char[] array2 = { 'a', 'b', 'c' };
                char[] array3 = { 'a', 'b', 'c' };
                System.out.println(Arrays.equals(array2, array3));
                String str1 = new String(array2);
                String str2 = new String(array3);
                System.out.println(str1.equals(str2));

                /**
                 *  Use the dichotomy in an array for the specified element if found returns the element with index  , If it returns a negative number does not exist
                 *
                 *  Use an array of binary lookup must be sorted
                 */
                System.out.println(Arrays.binarySearch(array1, 4));

                /**
                 *  Copies an array
                 *
                 * arraycopy( The source array, beginning of the source array  ,  The destination array, the start position of the target array.  ,  Copy several elements  );
                 */
                int[] array4 = { 1, 2, 3, 4, 5 };
                int[] array5 = new int[5];
                System.arraycopy(array4, 1, array5, 0, 3);
                iterator(array5);

                /**
                 * JDK6.0 Copies an array
                 */
                int[] array6 = new int[7];
                array6 = Arrays.copyOf(array4, array4.length);
                iterator(array6);
        }
}

Joseph array to solve the problem

/**
 *  Joseph problem
 */
package org.sean.module02;

/**
 * @author  Sun  (Sean Yang)
 */
public class Count3Quit {

        /**
         *  First of all determine array inside element is true, that is, whether in the ring  , If true then continue to count  , A counter incremented
         *  When the counter reaches 3 times  , First of all counters to zero, and then  boolean The location in the array of elements to be reset to false, the total number is reduced by one at the same time  , Index index increase a
         *  Because many people in the end, so when you get to the last element of time  , Index to zero
         *
         * @param array
         *             The array you want to test
         */
        public static void count3Quit(boolean[] array) {
                for (int i = 0; i < array.length; i++) {
                        array[i] = true;
                }

                //  Counter
                int counter = 0;
                //  The number of remaining
                int leftCount = array.length;
                //  Index
                int index = 0;

                while (leftCount > 1) {
                        if (array[index] == true) {
                                counter++;
                                if (counter == 3) {
                                        counter = 0;
                                        array[index] = false;
                                        leftCount--;
                                }
                        }
                        index++;
                        if (index == array.length) {
                                index = 0;
                        }
                }

                for (int i = 0; i < array.length; i++) {
                        if (array[i] == true) {
                                System.out.println(" The remaining staff position is  " + (i + 1));
                        }
                }
        }

        public static void main(String[] args) {
                boolean[] array = new boolean[500];
                count3Quit(array);
        }
}
相关文章
  • Java SE 02 Code Study Notes 2010-05-08

    02 code with study notes Array of related API /** * The test class Arrays */ package org.sean.module02; import java.util.Arrays; /** * @author Sun (Sean Yang) */ public class TestArrays { /** * Integer array output * * @param array */ public static v

  • java interview questions 1 Study Notes 2010-07-17

    1, JSP's built-in objects and their role A: JSP total of the following nine kinds of basic built-in component (ASP-6 with the corresponding internal components): request the client request, this request will be included from the GET / POST request pa

  • Java Collections Framework _ study notes 1 (ArrayList articles) 2010-10-24

    Under this sub-Java collection framework From Collection to List, Set, Map These are the set of defined interfaces: Now from the ArrayList implementation to begin with. Code analysis: a) constructor: ArrayList () There are three ways, first, construc

  • Java virtual machine in-depth study notes 2010-12-27

    java virtual machine: java virtual machine includes a classloader and an execution engine. Execution Engine: Execution engine to achieve diverse, there are 4 mainstream 1: to explain the implementation of the simplest means of implementation. 2: just

  • Servlet source code study notes -1 2010-10-31

    One. The key class description: 1. servlet general categories: public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable / / Servlet container will call this method to complete the initialization of servlet / / Conf

  • Java code reuse (SSH Study Notes 1) 2010-07-05

    Often and for a code dealing with people, code reuse is very important. When we develop a system inevitably will often write a function very similar code, which will be managed if the code makes the program become very bloated, so that the maintenanc

  • Effective Java 2 study notes Chapter 5 Generic 2009-01-15

    Article 23-29 of that chapter, focuses on the use of generic programming to the safety benefits as well as the API can be more widely used. 23: Do not use the original code in the new ecological types Using the original eco-types, such as list, you c

  • Effective Java 2 study notes and Chapter 6, enumeration and annotations 2010-03-25

    Article 30: Use enum instead of int constants Enumeration type comparison int enumeration, String enumeration, etc. Easy to read (different enumeration can contain the same name, etc.) More secure (different enumeration values can not be inter-operat

  • java generics Study Notes 2010-08-11

    java generics started out from the JDK1.5, although he usually has used before, but has not been a comprehensive study looking at myibatis3.0.1 source today, I saw many uses generic design, so its the generic learning the next. What is generic? Prior

  • 10 struts2 java study notes 2010-10-28

    Recently I have spare time Java Virtual Machine Research, the intern assigned to the project team inside, do not want it used to be free, and managed to find time to continue the topic post. I intend to end some of J2ee and then talk about the JVM an

  • java reference personal study notes 2011-08-14

    Today, when looking at the blog to the java reference to learn the knowledge, knowledge is a personal study notes, reference blog is: http://www.ibm.com/developerworks/cn/java/j-lo-langref/index.html?ca=drs- hope that you will Paizhuan, so I can lear

  • Java IO study notes: Concepts and Principles 2010-03-29

    Java IO study notes: Concepts and Principles First, the concept of Java in the operation of the file is streaming manner. Flow is a set of Java in-memory ordered data sequence. Java data from the source (file, memory, keyboard, network) read into the

  • Study Notes Oracle Database 02 2010-05-04

    Oracle study notes of the two - SQL Language Fundamentals 1.SQL language classification a. Data Query Language (Select statement): used to retrieve data, features, and more complex grammar, but also the most widely used b. Data Manipulation Language

  • UC + + Study Notes code 2010-08-21

    UC + + Study Notes Continuously updated. . .

  • Memcached Study Notes - Part IV: Memcached Java client-gwhalin (3)-Spring integration 2011-07-09

    Memcached Java Client Spring Integration If your project is to use Spring as the middleware, the configuration of Spring's integration is useful. Profile applicationContext-memcachedjavaclient.xml <?xml version="1.0" encoding="UTF-8"

  • Java SE technology 2009-09-09

    What is Java technology? Java technology is both a high-level object-oriented programming language, but also a platform. Java technology is based on the Java Virtual Machine (Java virtual machine, JVM) concept - this is the language and the underlyin

  • [Change] JdbcTemplate Study Notes 2010-04-06

    The article was from: http://it.oyksoft.com/post/234/ JdbcTemplate Study Notes 1, using JdbcTemplate's execute () method implementation of the SQL statement Java code jdbcTemplate.execute ( "CREATE TABLE USER (user_id integer, name varchar (100 ))&qu

  • lucene 3.0 Study Notes (1) - index 2009-09-25

    Are learning lucene, download the new version is 3.0, where the study notes in order and put in as a backup blog. Use lucene as a search engine, the main two things to do are: 1, indexing; 2, using the index query. That lucene first to search the con

  • javascript Collection study notes 107 2009-10-05

    js Collection study notes, record the contents of the JAVA SCRIPT Collection 1.document.write (""); Output Statement 2.JS in the comments for / / 3. The traditional sequence of HTML documents: document-> html-> (head, body) 4. A browser wi

  • Spring in Action Study Notes - Chapter IV conquest database (change) 2008-09-28

    Spring2.0 official version (http://www.springframework.org/download) Spring2.0 Chinese documents (http://spring.jactiongroup.net/viewtopic.php?t=2279&sid=0906ae6a913537b249e501db5b54c181). In the previous study, we have learned the core Spring framew