本文目录一览:
一个js方法转换为java代码
package p1;
import java.util.Arrays;
import java.util.LinkedList;
public class Guy
{
public static void recursionSub ( LinkedListint[] list, int count, int[] array, int ind, int start, int... indexs )
{
start++;
if (start count - 1)
{
return;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs = ind; indexs array.length; indexs++ )
{
recursionSub (list, count, array, indexs + 1, start, indexs);
if (start == count - 1)
{
int[] temp = new int[count];
for ( int i = count - 1; i = 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
list.add (temp);
}
}
}
public static void main ( String[] args )
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
LinkedListint[] list = new LinkedListint[] ();
recursionSub (list, 3, array, 0, -1);
for ( int[] strings : list )
{
System.out.println (Arrays.toString (strings));
}
}
}
如何把js正则改为java正则
JAVA好像真不能直接和JS的一样,不过我写了其他的办法,就当是从JS转成了JAVA版的吧,呵呵
public
class
Test
{
public
static
void
main(String[]
args)
{
System.out.println(getResults("12a4.678"));
}
public
static
int
getResults(String
s){
char
c[]
=
s.toCharArray();
if(s.length()
4){
return
0;
}
int
ls
=
0;
if
(matches(c,
"^[a-z]+$")){
ls++;
}
if
(matches(c,
"^[0-9]+$")){
ls++;
}
if
(matches(c,
"^[.[^a-z0-9]]+$")){
ls++;
}
if
(s.length()
6
ls
0){
ls--;
}
return
ls;
}
public
static
boolean
matches(char
c[],
String
regex){
for
(int
i
=
0;
i
c.length;
i++)
{
if(String.valueOf(c[i]).matches(regex)){
return
true;
}
}
return
false;
}
}
js代码转换为java
理解他的原理就好写了。
所有参数 排除 时间戳,按照字母顺序排序,然后连接起来 和 appKey 以及 appSercet 进行md5 运算。
排序可以使用 String []keys = map.keyset().toarray() 获取后使用 Collections 进行sort。
有个类似的,可以参考一下:
public static boolean signVerify(String appSecret,MapString, String params){
MapString, String map=new HashMapString, String();
map.put("appSecret", appSecret);
for(String key:params.keySet()){
if(!key.equals("sign")){
map.put(key, params.get(key));
}
}
String sign=sign(map);
if(sign.equals(params.get("sign"))){
return true;
}
return false;
}
private static String toHexValue(byte[] messageDigest) {
if (messageDigest == null)
return "";
StringBuilder hexValue = new StringBuilder();
for (byte aMessageDigest : messageDigest) {
int val = 0xFF aMessageDigest;
if (val 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
*
* @param params
* @return
*/
public static String sign(MapString,String params){
ListString keys=new ArrayListString(params.keySet());
Collections.sort(keys);
String string="";
for(String s:keys){
string+=params.get(s);
}
String sign="";
try {
sign = toHexValue(encryptMD5(string.getBytes(Charset.forName("utf-8"))));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("md5 error");
}
return sign;
}
private static byte[] encryptMD5(byte[] data)throws Exception{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(data);
return md5.digest();
}
public static void main(String[] args) {
String appKey="key";
String appSecret="secret";
MapString, String params=new HashMapString, String();
params.put("appKey", appKey);
params.put("appSecret", appSecret);
params.put("date", new Date().getTime()+"");
String sign=sign(params);
params.put("sign", sign);
System.out.println(signVerify(appSecret, params));
}
js转java代码
package p1;
import java.util.Arrays;
import java.util.LinkedList;
public class Guy
{
public static void recursionSub ( LinkedListint[] list, int count, int[] array, int ind, int start, int... indexs )
{
start++;
if (start count - 1)
{
return;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs = ind; indexs array.length; indexs++ )
{
recursionSub (list, count, array, indexs + 1, start, indexs);
if (start == count - 1)
{
int[] temp = new int[count];
for ( int i = count - 1; i = 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
list.add (temp);
}
}
}
public static void main ( String[] args )
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
LinkedListint[] list = new LinkedListint[] ();
recursionSub (list, 3, array, 0, -1);
for ( int[] strings : list )
{
System.out.println (Arrays.toString (strings));
}
}
}