日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

過來的經(jīng)典東東

系統(tǒng) 1752 0
月29日

一個(gè)帳號(hào)同一時(shí)間只能一人登錄

對(duì)于一個(gè)帳號(hào)在同一時(shí)間只能一個(gè)人登錄,可以通過下面的方法實(shí)現(xiàn):

1 .在用戶登錄時(shí),把用戶添加到一個(gè) ArrayList

2 .再次登錄時(shí)查看 ArrayList 中有沒有該用戶,如果 ArrayList 中已經(jīng)存在該用戶,則阻止其登錄

3 .當(dāng)用戶退出時(shí),需要從該 ArrayList 中刪除該用戶,這又分為三種情況

使用注銷按鈕正常退出

點(diǎn)擊瀏覽器關(guān)閉按鈕或者用 Alt+F4 退出,可以用 javascript 捕捉該頁面關(guān)閉事件,

執(zhí)行一段 java 方法刪除 ArrayList 中的用戶

非正常退出,比如客戶端系統(tǒng)崩潰或突然死機(jī),可以采用隔一段時(shí)間 session 沒活動(dòng)就刪除該 session 所對(duì)應(yīng)的用戶來解決,這樣用戶需要等待一段時(shí)間之后就可以正常登錄。

?

LoginAction 中定義:

// 用來在服務(wù)器端存儲(chǔ)登錄的所有帳號(hào)

public static List logonAccounts;

?

login() 登錄方法中:

// 設(shè)置 session 不活動(dòng)時(shí)間為 30

request.getSession().setMaxInactiveInterval(60*30);

if(logonAccounts==null){

??? logonAccounts = new ArrayList();

}

// 查看 ArrayList 中有沒有該用戶

if(!logonAccounts.contains(account.getAccountId())){

??? // 在用戶登錄時(shí),把用戶添加到一個(gè) ArrayList

??? logonAccounts.add(account.getAccountId());

??? return "login";

}else{

??? return "denied";

}

?

使用注銷按鈕正常退出

logout() 退出方法中:

if(logonAccounts==null){

??? logonAccounts = new ArrayList();

}

if(logonAccounts.contains(account.getAccountId())){

??? logonAccounts.remove(account.getAccountId());

}

?

點(diǎn)擊瀏覽器關(guān)閉按鈕或者用 Alt+F4 退出:

在后臺(tái)彈出一個(gè)窗口,在彈出窗口中刪除 ArrayList 中的用戶

function window.onbeforeunload(){

??? window.open('accountUnbound.jsp','',

??????????? 'height=0,width=0,top=10000,left=10000')

}

?

accountUnbound.jsp : 彈出窗口中刪除 ArrayList 中的用戶

<%

??? Account account = (Account) request.getSession().getAttribute("account");

??? if(account != null){

??????? if(LoginAction.logonAccounts==null){

??????????? LoginAction.logonAccounts = new ArrayList();

??????? }

??????? if(LoginAction.logonAccounts.contains(account.getAccountId())){

??????????? LoginAction.logonAccounts.remove(account.getAccountId());

??????? }

??? }

%>

為了保證上面代碼可以執(zhí)行完畢, 3 秒后關(guān)閉此彈出窗口

<script>

setTimeout("closeWindow();",3000);

function closeWindow(){

??? window.close();

}

</script>

?

使 implements HttpSessionListener ,并實(shí)現(xiàn) sessionCreated/sessionDestroyed 方法

sessionDestroyed 中刪除 ArrayList 中的用戶(用戶超過 30 分鐘不活動(dòng)則執(zhí)行此方法)

??? Account account = (Account) request.getSession().getAttribute("account");

??? if(account != null){

??????? if(LoginAction.logonAccounts==null){

??????????? LoginAction.logonAccounts = new ArrayList();

??????? }

??????? if(LoginAction.logonAccounts.contains(account.getAccountId())){

??????????? LoginAction.logonAccounts.remove(account.getAccountId());

??????? }

??? }

?

注:

對(duì)于上面的,由于彈出窗口很容易被防火墻或者安全軟件阻攔,造成無法彈出窗口,從而短時(shí)間不能登錄,這種情況可以用 AJAX 來代替彈出窗口,同樣在后臺(tái)執(zhí)行刪除用戶的那段代碼,卻不會(huì)受到防火墻限制:

<script>

??? // <![CDATA[

??? var http_request = false;

??? function makeRequest (url) {

??????? http_request = false;

??????? if (window.XMLHttpRequest) { // Mozilla, Safari,...

??????????? http_request = new XMLHttpRequest();

??????????? if (http_request.overrideMimeType) {

??????????????? http_request.overrideMimeType('text/xml');

??????????? }

??????? } else if (window.ActiveXObject) { // IE

??????????? try {

??????????????? http_request = new ActiveXObject("Msxml2.XMLHTTP");

??????????? } catch (e) {

??????????????? try {

??????????????????? http_request = new ActiveXObject("Microsoft.XMLHTTP");

??????????????? } catch (e) {

??????????????? }

??????????? }

??????? }

??????? if (!http_request) {

??????????? alert('Giving up :( Cannot create an XMLHTTP instance');

??????????? return false;

??????? }

??????? http_request.onreadystatechange = alertContents;

??????? http_request.open('GET', url, true);

??????? http_request.send(null);

?

??? }

??? function alertContents() {

??????? if (http_request.readyState == 4) {

??????????? if (http_request.status == 200) {

??????????????? window.close();

??????????? } else {

??????????????? alert('There was a problem with the request.');

??????????? }

??????? }

?

??? }

??? function window. onbeforeunload() {

??????? makeRequest ('accountUnbound.jsp');

??? }

??? //]]>

</script>

?

對(duì)于上面的這段 ajax 代碼,在網(wǎng)上有很多詳細(xì)的解釋,把它加到 onbeforeunload() 瀏覽器關(guān)閉事件中,在后臺(tái)執(zhí)行代碼的效果很好, 不必?fù)?dān)心彈出窗口有時(shí)候會(huì)無效的問題

?

使用這段代碼后,上面 accountUnbound.jsp 中的那段關(guān)閉彈出窗口 window.close(); js 代碼就不需要了。

5月31日

樹形菜單一

大家知道,樹型菜單在應(yīng)用中有著十分廣泛的用途。實(shí)現(xiàn)樹型菜單的途徑較多,本文介紹的一種覺得理解起來比較直觀,與上篇文章的方法比較類似:就是將樹型菜單的節(jié)點(diǎn)保存在數(shù)據(jù)庫表中(當(dāng)然,在實(shí)際項(xiàng)目中,節(jié)點(diǎn)的信息往往并不是放在一個(gè)單一的表中的。比如:在一個(gè)權(quán)限管理系統(tǒng)中,這些信息可能分別放在用戶表、角色表、功能表等表中,只要設(shè)法讓查詢出來的結(jié)果與下面給出的表格的內(nèi)容相似就可以了。只要稍微有些數(shù)據(jù)庫方面的知識(shí)做到這點(diǎn)并不難,詳細(xì)的實(shí)現(xiàn)細(xì)節(jié)超出了本文的主題,不在此細(xì)說)。通過數(shù)據(jù)訪問對(duì)象將其從數(shù)據(jù)庫中查出后放在一個(gè)集合對(duì)象中,并將該集合對(duì)象傳遞給客戶端,再用一段現(xiàn)存的JavaScript代碼--dtree(一個(gè)免費(fèi)的JavaScript程序)來操作集合中的數(shù)據(jù)。大方向確定之后,我們就來具體著手來實(shí)現(xiàn)它。

????根據(jù)dtree的要求,我們來建一個(gè)數(shù)據(jù)庫表來存儲(chǔ)樹的節(jié)點(diǎn)信息,表名為functions,其結(jié)構(gòu)如下:

                  id字段:varchar 10 主鍵--節(jié)點(diǎn)標(biāo)識(shí)碼
pid字段:varchar 10 not null--父節(jié)點(diǎn)標(biāo)識(shí)碼
name字段:varchar 20 not null
url字段:varchar 50 not
null--這個(gè)字段存儲(chǔ)的是點(diǎn)擊該節(jié)點(diǎn)時(shí),要定位的資源(比如一個(gè)頁面的url),
為了不使本文的篇幅過長,暫時(shí)不給出相應(yīng)的頁面,
您可以隨便輸入一個(gè)字母比如:a,以使本例能夠正常運(yùn)行。
title字段:varchar 20
target字段:varchar 10
icon字段:varchar 20
iconopen字段:varchar 20
opened字段:char 1
                


????在表中輸入如下一些記錄以供后面的實(shí)驗(yàn)用:

                  0、-1、我的權(quán)限、javascript: void(0);
00、0、用戶管理、javascript: void(0);
0001、00、創(chuàng)建新用戶;
0002、00、刪除用戶;
01、0、	文章管理、javascript: void(0);
0101、01、添加新文章;
0102、01、修改文章;
0103、01、刪除文章;
                


????到此,數(shù)據(jù)庫方面的準(zhǔn)備工作就告一段落。

????接下來的工作我們?nèi)匀辉谙惹敖榻B的mystruts項(xiàng)目中進(jìn)行。先編寫一個(gè)名為:FunctionsForm的ActionForm,其代碼如下:

                  package entity;
import org.apache.struts.action.*;
import javax.servlet.http.*;

public class FunctionsForm extends ActionForm {
  private String icon;
  private String iconOpen;
  private String id;
  private String name;
  private String opened;
  private String pid;
  private String target;
  private String title;
  private String url;
  public String getIcon() {
    return icon;
  }
  public void setIcon(String icon) {
    this.icon = icon;
  }
  public String getIconOpen() {
    return iconOpen;
  }
  public void setIconOpen(String iconOpen) {
    this.iconOpen = iconOpen;
  }
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getOpened() {
    return opened;
  }
  public void setOpened(String opened) {
    this.opened = opened;
  }
  public String getPid() {
    return pid;
  }
  public void setPid(String pid) {
    this.pid = pid;
  }
  public String getTarget() {
    return target;
  }
  public void setTarget(String target) {
    this.target = target;
  }
  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public String getUrl() {
    return url;
  }
  public void setUrl(String url) {
    this.url = url;
  }
}
                


????因?yàn)槲覀兊臉湫凸?jié)點(diǎn)的數(shù)據(jù)都存儲(chǔ)在數(shù)據(jù)庫表中,接下來,要做一個(gè)數(shù)據(jù)訪問對(duì)象類,名稱為:FunctionsDao.java,其代碼如下:

                  package db;
import java.sql.*;
import java.util.*;
import entity.FunctionsForm;

public class FunctionsDao {
  private static Connection con = null;

  public FunctionsDao(Connection con) {
    this.con=con;
  }

  public static Collection findTree() {
    PreparedStatement ps=null;
    ResultSet rs = null;
    ArrayList list=new ArrayList();

    String sql="select * from functions";

    try{
      if(con.isClosed()){
        throw new IllegalStateException("error.unexpected");

      }
      ps=con.prepareStatement(sql);

      rs=ps.executeQuery();
      while(rs.next()){
        FunctionsForm functionsForm=new FunctionsForm();
        functionsForm.setId(rs.getString("id"));
        functionsForm.setPid(rs.getString("pid"));
        functionsForm.setName(rs.getString("name"));
        functionsForm.setUrl(rs.getString("url"));
        functionsForm.setTitle(rs.getString("title"));
        functionsForm.setTarget(rs.getString("target"));
        functionsForm.setIcon(rs.getString("icon"));
        functionsForm.setIconOpen(rs.getString("iconOpen"));
        functionsForm.setOpened(rs.getString("opened"));
        list.add(functionsForm);

      }
      return list;
    }
    catch(SQLException e){
        e.printStackTrace();
        throw new RuntimeException("error.unexpected");
    }
    finally{
      try{
        if(ps!=null)
          ps.close();
        if(rs!=null)
          rs.close();
      }catch(SQLException e){
        e.printStackTrace();
        throw new RuntimeException("error.unexpected");
      }
    }
  }
}
                


????這里值得注意的是:在以往我們見到的一些顯示樹型菜單的程序,如:一些asp程序中往往簡單地采用遞歸調(diào)用的方法來查找到樹的各個(gè)節(jié)點(diǎn)。這對(duì)那些樹的深度不確定的場合還是有些用處,但這種處理方法也有一個(gè)致命的弱點(diǎn),那就是反復(fù)地進(jìn)行數(shù)據(jù)庫查詢,對(duì)一些節(jié)點(diǎn)較多的應(yīng)用,對(duì)應(yīng)用程序性能的影響是非常大的,有時(shí)會(huì)慢得讓人難以接受;而在實(shí)際的應(yīng)用中大多數(shù)情況下樹的深度往往是有限的,如:用于會(huì)計(jì)科目的樹一般最多也在六層以下。又如:用作網(wǎng)頁功能菜單的情況,網(wǎng)頁設(shè)計(jì)的原則就有一條是:達(dá)到最終目的地,鼠標(biāo)點(diǎn)擊次數(shù)最好不要多于三次。因此,在實(shí)際設(shè)計(jì)存儲(chǔ)樹型結(jié)構(gòu)的表時(shí)要考慮查詢的效率。對(duì)能確定樹的最大深度的情況下,要設(shè)法盡量優(yōu)化查詢語句,減少查詢次數(shù),以提高應(yīng)用程序的性能同時(shí)減少數(shù)據(jù)庫的負(fù)荷。

????本例對(duì)應(yīng)的Action的名稱為FunctionsAction,其代碼如下:

                  package action;

import entity.*;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import db.FunctionsDao;

public class FunctionsAction extends Action {
  public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
  HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 
  {
    DataSource dataSource;
    Connection cnn=null;
    ActionErrors errors=new ActionErrors();
    try{
      dataSource = getDataSource(httpServletRequest,"A");
      cnn = dataSource.getConnection();
      FunctionsDao functionsDao=new FunctionsDao(cnn);
      Collection col=functionsDao.findTree();
      httpServletRequest.setAttribute("treeList",col);

      return actionMapping.findForward("success");
    }
    catch(Throwable e){
      e.printStackTrace();
      //throw new RuntimeException("未能與數(shù)據(jù)庫連接");
      ActionError error=new ActionError(e.getMessage());
      errors.add(ActionErrors.GLOBAL_ERROR,error);
    }
    finally{
      try{
        if(cnn!=null)
          cnn.close();
      }
      catch(SQLException e){
        throw new RuntimeException(e.getMessage());
      }
    }
    saveErrors(httpServletRequest,errors);
    return actionMapping.findForward("fail");
  }
}
                


????在struts-config.xml文件中加入如下內(nèi)容:

                  <form-beans>    
    <form-bean name="functionsForm" type="entity.FunctionsForm" />
  </form-beans>
<action-mappings>
    <action name="functionsForm" path="/functionsAction" scope="request"
	type="action.FunctionsAction" validate="false" >
<forward name="success" path="/testDTree.jsp" />
<forward name="fail" path="/genericError.jsp" />
    </action>
  </action-mappings>
                


????為了對(duì)應(yīng)配置中的,我們還要提供一個(gè)顯示錯(cuò)誤信息的jsp頁面,其代碼如下:

                  <%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>
genericError
</title>
<link href="css/mycss.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#ffffff">
<html:errors/>
</body>
</html>
                


????下面,我們來看一下我們顯示樹型菜單的頁面代碼,從配置中可以看出,頁面的名稱為testDTree.jsp,代碼如下:

                  <%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<title>
testDTree
</title>
<link rel="StyleSheet" href="css/dtree.css" type="text/css" />
</head>
<body bgcolor="#eeeeee">
<body leftmargin="0" topmargin="0"><table width="180">
<tr><td height="300" valign="top" nowrap>
<script type="text/javascript" src="js/dtree.js"></script>
<script type='text/javascript'>
tree = new dTree('tree');
tree.config.folderLinks=false;
tree.config.useCookies=false;
<logic:iterate id="functionsForm" name="treeList" scope="request"
type="entity.FunctionsForm">
    tree.add("<bean:write name="functionsForm" property="id"/>","<bean:write
	name="functionsForm" property="pid"/>","<bean:write name="functionsForm"
	property="name"/>","<bean:write name="functionsForm"
	property="url"/>","<bean:write name="functionsForm"
	property="title"/>","<bean:write name="functionsForm"
	property="target"/>","<bean:write name="functionsForm" property="icon"/>");
</logic:iterate>
      document.write(tree);
</script>
    </td>
  </tr>
</table>
</body>
</html>
                


????從 可以看出,我們要在mystruts目錄下,建一個(gè)名為js的目錄,并將下載的dtree文件dtree.js放在該目錄中。

????再在mystruts目錄下分別建一個(gè)名為img和名為css的目錄,將dtree中用到的圖標(biāo)和層疊樣式表單文件分別放在相應(yīng)的目錄中。

????有關(guān)dtree的使用方法,詳見其說明文檔,如:api.html。筆者在此要感謝dtree的作者為我們提供了一個(gè)結(jié)構(gòu)如此清晰的javascript程序!

????現(xiàn)在,可以編譯執(zhí)行這個(gè)例子程序了,編譯后在瀏覽器中輸入:http://127.0.0.1:8080/mystruts/functionsAction.do就可以看到運(yùn)行效果。效果圖為:



????注:dtree的下載地址為: http://www.destroydrop.com/javascripts/tree/

使用hashtable對(duì)字符串操作

1.在一些字符串?dāng)?shù)組中,常會(huì)有重復(fù)的記錄,比如手機(jī)號(hào)碼,我們可以通過Hashtable來對(duì)其進(jìn)行過濾

public ?String[]?checkArray(String[]?str) ... {
????????Hashtable
< String,?String > ?hash = new ?Hashtable < String,?String > ();

????????
for ( int ?i = 0 ;i < str.length;i ++ ) ... {
????????????
if ( ! hash.containsKey(str[i]))
????????????????hash.put(str[i],?str[i]);
????????}


????????Enumeration?enumeration
= hash.keys();
????????String[]?str_new
= new ?String[hash.size()];
????????
int ?i = 0 ;

????????
while (enumeration.hasMoreElements()) ... {
????????????str_new[i]
= enumeration.nextElement().toString();
????????????i
++ ;
????????}

????????
return ?str_new;
????}

示例:
??? ??? String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};
??? ??? mobile=checkArray(mobile);
??? ??? for(int i=0;i<mobile.length;i++)
??? ??? ??? System.out.println(mobile[i]);
??? ?? 輸出結(jié)果為:
??? ??? 13811071503
??? ??? 13811071501
??? ??? 13811071500
2.A,B均為字符串?dāng)?shù)組,找出在A中存在,而在B中不存在的字符串
??? public String[] compareArray(String[] A,String[] B){
??? ??? Hashtable<String, String> hash=new Hashtable<String, String>();
??? ??? Hashtable<String, String> hash_new=new Hashtable<String, String>();

??? ??? for(int i=0;i<B.length;i++)
??? ??? ??? hash.put(B[i], B[i]);

??? ??? for(int i=0;i<A.length;i++){
??? ??? ??? if(!hash.containsKey(A[i]))
??? ??? ??? ??? hash_new.put(A[i], A[i]);
??? ??? }

??? ??? String[] C=new String[hash_new.size()];
??? ??? int i=0;
??? ??? Enumeration enumeration=hash_new.keys();

??? ??? while(enumeration.hasMoreElements()){
??? ??? ??? C[i]=enumeration.nextElement().toString();
??? ??? ??? i++;
??? ??? }
??? ??? return C;
??? }
示例:
??? ??? String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};
??? ??? String[] mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};
??? ??? String[] mobile3=compareArray(mobile1,mobile2);
??? ??? for(int i=0;i<mobile3.length;i++)
??? ??? ??? System.out.println(mobile[i]);
輸出結(jié)果:
??? 13811071503
??? 13811071501
存在的問題:
每次都是倒序,可以再對(duì)程序稍加改動(dòng),變成正序。

?

3.將一個(gè)字符串?dāng)?shù)組中某一個(gè)特定的字符串過濾掉

/**?*/ /** 檢驗(yàn)一個(gè)字符串?dāng)?shù)組,若包含某一特定的字符串,則將該字符串從數(shù)組中刪
除,返回剩余的字符串?dāng)?shù)組
?????*?
@param ?str_array??字符串?dāng)?shù)組
?????*?
@param ?str_remove?待刪除的字符串
?????*?
@return ?過濾后的字符串
?????
*/

????
public ?String[]?removeStrFromArray(String[]?str_array,String
str_remove)
... {
????????Hashtable
< String,?String > ?hash = new ?Hashtable < String,?String > ();
????????
for ( int ?i = 0 ;i < str_array.length;i ++ ) ... {
????????????
if ( ! str_array[i].equals(str_remove))
????????????????hash.put(str_array[i],?str_array[i]);
????????}

????????
// 生成一個(gè)新的數(shù)組
????????String[]?str_new = new ?String[hash.size()];
????????
int ?i = 0 ;
????????Enumeration?enumeration
= hash.keys();
????????
while (enumeration.hasMoreElements()) ... {
????????????str_new[i]
= enumeration.nextElement().toString();
????????????i
++ ;
????????}

????????
return ?str_new;
????}

5月29日

java隨機(jī)數(shù)

在Java中我們可以使用java.util.Random類來產(chǎn)生一個(gè)隨機(jī)數(shù)發(fā)生器。它有兩種形式的構(gòu)造函數(shù),分別是Random()和Random(long seed)。Random()使用當(dāng)前時(shí)間即System.currentTimeMillis()作為發(fā)生器的種子,Random(long seed)使用指定的seed作為發(fā)生器的種子。

??????? 隨機(jī)數(shù)發(fā)生器(Random)對(duì)象產(chǎn)生以后,通過調(diào)用不同的method:nextInt()、nextLong()、nextFloat()、nextDouble()等獲得不同類型隨機(jī)數(shù)。

?????? 1>生成隨機(jī)數(shù)
?? ??????? Random random = new Random();
?????????? Random random = new Random(100);//指定種子數(shù)100
?????????? random調(diào)用不同的方法,獲得隨機(jī)數(shù)。
?????????? 如果2個(gè)Random對(duì)象使用相同的種子(比如都是100),并且以相同的順序調(diào)用相同的函數(shù),那它們返回值完全相同。如下面代碼中兩個(gè)Random對(duì)象的輸出完全相同
????????? import java.util.*;
????????? class TestRandom {
??????????????? public static void main(String[] args) {
???????????????????? Random random1 = new Random(100);
???????????????????? System.out.println(random1.nextInt());
???????????????????? System.out.println(random1.nextFloat());
?????????????????????System.out.println(random1.nextBoolean());
???????????????????? Random random2 = new Random(100);
???????????????????? System.out.println(random2.nextInt());
???????????????????? System.out.println(random2.nextFloat());
???????????????????? System.out.println(random2.nextBoolean());
??????????????? }
??????????? }

????????2>指定范圍內(nèi)的隨機(jī)數(shù)
???????????? 隨機(jī)數(shù)控制在某個(gè)范圍內(nèi),使用模數(shù)運(yùn)算符%
??????????? import java.util.*;
???????????????? class TestRandom {
????????????????????? public static void main(String[] args) {
?????????????????????????? Random random = new Random();
?????????????????????????? for(int i = 0; i < 10;i++) {
?????????????????????????????? System.out.println(Math.abs(random.nextInt())%10);
?????????????????????????? }
????????????????????? }
???????????????? }
???????????? 獲得的隨機(jī)數(shù)有正有負(fù)的,用Math.abs使獲取數(shù)據(jù)范圍為非負(fù)數(shù)

???????3>獲取指定范圍內(nèi)的不重復(fù)隨機(jī)數(shù)
?????????? ?import java.util.*;
????????????class TestRandom {
????????????????? public static void main(String[] args) {
???????????????????????int[] intRet = new int[6];?
?????????????????????? int intRd = 0; //存放隨機(jī)數(shù)
?????????????????????? int count = 0; //記錄生成的隨機(jī)數(shù)個(gè)數(shù)
?????????????????????? int flag = 0; //是否已經(jīng)生成過標(biāo)志
?????????????????????? while(count<6){
??????????????????????????? Random rdm = new Random(System.currentTimeMillis());
??????????????????????????? intRd = Math.abs(rdm.nextInt())%32+1;
?????????????????????????? ?for(int i=0;i<count;i++){
?????????????????????????????? ?if(intRet[i]==intRd){
??????????????????????????????????? flag = 1;
??????????????????????????????????? break;
????????????????????????????????}else{
??????????????????????????????????? flag = 0;
????????????????????????????????}
??????????????????????????? }
??????????????????????????? if(flag==0){
??????????????????????????????? intRet[count] = intRd;
??????????????????????????????? count++;
????????????????????????????}
?????????????????? }
????????????????? for(int t=0;t<6;t++){
????????????????????? System.out.println(t+"->"+intRet[t]);
??????????????????}
?????????????? }
??????????? }
Java中的隨機(jī)數(shù)是否可以重復(fù)?Java中產(chǎn)生的隨機(jī)數(shù)能否可以用來產(chǎn)生數(shù)據(jù)庫主鍵?帶著這個(gè)問題,我們做了一系列測試。
1.測試一: 使用不帶參數(shù)的Random()構(gòu)造函數(shù)

public class RandomTest? {

? public static void main(Str

過來的經(jīng)典東東


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 延安市| 瓮安县| 恩平市| 达州市| 西藏| 丽江市| 澎湖县| 无棣县| 万源市| 安吉县| 唐山市| 平南县| 宜州市| 泰州市| 高阳县| 万盛区| 邯郸市| 富顺县| 特克斯县| 广汉市| 和静县| 康平县| 普格县| 镇安县| 泾阳县| 扎兰屯市| 图片| 洮南市| 政和县| 东莞市| 台前县| 乐昌市| 松滋市| 洮南市| 龙江县| 比如县| 县级市| 静安区| 阿拉善盟| 邯郸县| 垦利县|