顯示具有 java 標籤的文章。 顯示所有文章
顯示具有 java 標籤的文章。 顯示所有文章

2016年2月7日 星期日

java-sort


source :

http://mathbits.com/MathBits/Java/arrays/SelectionSort.htm



Array at beginning: 846976869491
After Pass #1:849176869469
After Pass #2:849194867669
After Pass #3: 869194847669
After Pass #4: 949186847669
After Pass #5 (done): 949186847669

While being an easy sort to program, the selection sort is one of the least efficient.  The algorithm offers no way to end the sort early, even if it begins with an already sorted list.  

// Selection Sort Method for Descending Orderpublic static void SelectionSort ( int [ ] num )
{
     int i, j, first, temp; 
     for ( i = num.length - 1; i > 0; i - - ) 
     {
          first = 0;  
 //initialize to subscript of first element
          for(j = 1; j <= i; j ++)   
//locate smallest element between positions 1 and i.
          {
               if( num[ j ] < num[ first ] )         
                 first = j;
          }
          temp = num[ first ];   //swap smallest found with element in position i.
          num[ first ] = num[ i ];
          num[ i ] = temp;
      }          
}

 


Convert int[] into ArrayList


*****************************************
* String Array to Array List ******************
*****************************************

http://www.tutorialspoint.com/java/util/arrays_aslist.htm

Example

The following example shows the usage of java.util.Arrays.asList() method.
package com.tutorialspoint;

import java.util.Arrays;
import java.util.List;

public class ArrayDemo1 {
   public static void main (String args[]) {

   // create an array of strings
   String a[] = new String[]{"abc","klm","xyz","pqr"};
   
   List list1 = Arrays.asList(a);

   // printing the list
   System.out.println("The list is:" + list1);
   }
}
Let us compile and run the above program, this will produce the following result:
The list is:[abc, klm, xyz, pqr]


http://stackoverflow.com/questions/10530353/convert-string-array-to-arraylist



import java.util.Arrays;  
import java.util.List;  
import java.util.ArrayList;  
public class StringArrayTest  
{  
   public static void main(String[] args)  
   {  
      String[] words = {"ace", "boom", "crew", "dog", "eon"};  

      List<String> wordList = Arrays.asList(words);  

      for (String e : wordList)  
      {  
         System.out.println(e);  
      }  
   }  
}




**********************************
* Array List to string array ************
**********************************



List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
    System.out.println(s);




****************************************************************
************** Array List to Integer * *******************************
****************************************************************

http://www.tutorialspoint.com/java/util/arraylist_get.htm

Example

The following example shows the usage of java.util.ArrayList.get() method.
package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
   // create an empty array list with an initial capacity
   ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

   // use add() method to add elements in the list
   arrlist.add(15);
   arrlist.add(22);
   arrlist.add(30);
   arrlist.add(40);

   // let us print all the elements available in list
   for (Integer number : arrlist) {
   System.out.println("Number = " + number);
   } 
 
   // retrieves element at 4th postion
   int retval=arrlist.get(3);
   System.out.println("Retrieved element is = " + retval);    
   }
}   
Let us compile and run the above program, this will produce the following result:
Number = 15
Number = 22
Number = 30
Number = 40
Retrieved element is = 40


***********************************************************
*  int list to int[] *********************************************
***********************************************************
https://www.blogger.com/blogger.g?blogID=5452307390516932808#editor/target=post;postID=4395252080140059040


private int[] buildIntArray(List<Integer> integers) {
    int[] ints = new int[integers.size()];
    int i = 0;
    for (Integer n : integers) {
        ints[i++] = n;
    }
    return ints;
}




http://stackoverflow.com/questions/10269300/convert-int-into-arraylist



List<Integer> list = Arrays.asList(1, 2, 3);

So something along the lines of:

int[] array = { 1, 2, 3 };
ArrayList<Integer> list = new ArrayList<Integer>(array.length);
for (int i = 0; i < array.length; i++)
  list.add(Integer.valueOf(array[i]));






















2015年1月11日 星期日

java-right

java-money format


static String num2money(String money,int length){
String r="";
String endOfBacket="";
if(money.indexOf("-")>=0){
money=money.replace("-","");money="("+money;endOfBacket=")";
}
money="       "+money;
money=money.substring(money.length()-11,money.length())+endOfBacket;
r=money;
return  r;
}

2014年11月1日 星期六

java-csv


http://stackoverflow.com/questions/21378773/create-and-download-csv-file-javaservlet

create and download
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
    response.setContentType("text/csv");
    response.setHeader("Content-Disposition", "attachment; filename=\"userDirectory.csv\"");
    try
    {
        OutputStream outputStream = response.getOutputStream();
        String outputResult = "xxxx, yyyy, zzzz, aaaa, bbbb, ccccc, dddd, eeee, ffff, gggg\n";
        outputStream.write(outputResult.getBytes());
        outputStream.flush();
        outputStream.close();
    }
    catch(Exception e)
    {
        model.closeConnection();
        System.out.println(e.toString());
    }
}

2014年10月27日 星期一

javabean 20141021

e.g. ViewPDF.java

Servernet : PDFCreateAndView

import com.erp.utils.CompanyProfile;

public void processRequest(HttpServletRequest request,
HttpServletResponse response) {

CompanyProfile bean = (CompanyProfile) request.getSession().getAttribute("bean");
response.setContentType(this.CONTENT_TYPE);
String recordID=request.getParameter("recordid");
}


2014年7月23日 星期三

java-tab


java tab remove
java enter next line remove

  1. inputString = inputString.replaceAll("\t""");  
  2. inputString = inputString.replaceAll("\n""");  

2014年7月21日 星期一

java-bean servlet


servlet

e.g. PDFCreateAndView.java
CompanyProfile bean = (CompanyProfile) request.getSession().getAttribute("bean");
allDocGenPDF.logoSizeFactor=bean.getLogoSizeFactor();





http://stackoverflow.com/questions/2663079/java-servlet-and-jsp-accessing-the-same-session-bean

The basic semantic tries to find an existing object using id and scope. If the object is not found it will attempt to create the object using the other attributes.
In other words,
<jsp:useBean id="user" scope="session" class="package.name.User"/>
would translate roughly into java as:
package.name.User user = (package.name.User)session.getAttribute("user");
if (user == null){
  user = new package.name.User();
  session.setAttribute("user", user);
}