在本文中,我们将详细介绍在HTML的各个方面,并为您提供关于Java中查找值的快速方法的相关解答,同时,我们也将为您带来关于ACM必备的快速输入输出(Java)、html5–在HTML52dCanva
在本文中,我们将详细介绍在HTML的各个方面,并为您提供关于Java中查找值的快速方法的相关解答,同时,我们也将为您带来关于ACM必备的快速输入输出(Java)、html5 – 在HTML 5 2d Canvas上下文中查找曲线上的点、java – 在列表中查找唯一值的快速方法、Java-将数组值分配给各个变量的快速方法的有用知识。
本文目录一览:- 在HTML(Java)中查找值的快速方法(html怎么查找)
- ACM必备的快速输入输出(Java)
- html5 – 在HTML 5 2d Canvas上下文中查找曲线上的点
- java – 在列表中查找唯一值的快速方法
- Java-将数组值分配给各个变量的快速方法
在HTML(Java)中查找值的快速方法(html怎么查找)
使用正则表达式,最简单的方法是获取网站HTML并在此标记内找到值(或与此相关的任何属性值):
<html> <head> [snip] <meta name="generator" value="thevalue i''m looking for" /> [snip]
答案1
小编典典取决于您需要构建(验证等)Http请求的复杂程度。这是我过去使用过的一种简单方法。
StringBuilder html = new StringBuilder();java.net.URL url = new URL("http://www.google.com/");BufferedReader input = null;try { input new BufferedReader( new InputStreamReader(url.openStream())); String htmlLine; while ((htmlLine=input.readLine())!=null) { html.appendLine(htmlLine); }}finally { input.close();}Pattern exp = Pattern.compile( "<meta name=\"generator\" value=\"([^\"]*)\" />");Matcher matcher = exp.matcher(html.toString());if(matcher.find()){ System.out.println("Generator: "+matcher.group(1));}
编译时可能会发现很多错别字。 (希望这不是功课)
ACM必备的快速输入输出(Java)
Acmer
- 手写Input类
- BufferedReader
- StringTokenizer
- 静态Input方法
- StreamTokenizer
手写Input类
BufferedReader
从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取。
可以指定缓冲区大小,或者可以使用默认大小。 默认值足够大,可用于大多数用途。
通常,由读取器做出的每个读取请求将引起对底层字符或字节流的相应读取请求。
也可以直接使用 BufferedReader代替下面方法。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] stoken = br.readLine().split(" ");
int n = Integer.parseInt(stoken[0]);
int m = Integer.parseInt(stoken[1]);
String[] x = br.readLine().split(" ");
StringTokenizer
字符串tokenizer类允许应用程序将字符串拆分成令牌。
StreamTokenizer化方法比StreamTokenizer类使用的方法简单得多。
StringTokenizer方法不区分标识符,数字和引用的字符串,也不识别和跳过注释。
可以在创建时或每个令牌的基础上指定一组分隔符(分隔标记的字符)。
StringTokenizer一个实例以两种方式之一表现,具体取决于它是使用true或false的returnDelims标记创建的:
- 如果标志为false ,则分隔符用于分隔标记。 令牌是不是分隔符的连续字符的最大序列。
- 如果标志是true,则分隔符字符本身被认为是令牌。 因此,令牌是一个定界符字符,或不是分隔符的连续字符的最大序列。
A StringTokenizer对象内部维护要标记化的字符串中的当前位置。 某些操作会将当前位置提前处理。通过使用用于创建StringTokenizer对象的字符串的子字符串来返回令牌。
注意:
为指定的字符串构造一个字符串tokenizer。 标记器使用默认分隔符集,即" \t\n\r\f" :空格字符,制表符,换行符,回车字符和换页符。 分隔符字符本身不会被视为令牌。
class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String next() throws IOException {
while (tokenizer==null || !tokenizer.hasMoretokens()){
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public String nextLine() throws IOException {
return reader.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public Double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
测试代码:
public class quickInput {
public static void main(String[] args) {
try {
InputReader sc = new InputReader(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
long yy = sc.nextLong();
double xx = sc.nextDouble();
String ss = sc.nextLine();
System.out.println(x+y);
System.out.println(sc.nextInt());
System.out.println(yy);
System.out.println(xx);
System.out.println(ss);
} catch (Exception e){
System.out.println(e.toString());
}
}
}
静态Input方法
StreamTokenizer
StreamTokenizer类接收输入流并将其解析为“令牌”,允许一次读取一个令牌。 解析过程由表和多个可以设置为各种状态的标志来控制。 流标记器可以识别标识符,数字,引用的字符串和各种注释样式。
从输入流读取的每个字节被视为’\u0000’至’\u00FF’ 。 字符值用于查找字符的五个可能属性: 空格 , 字母 , 数字 , 字符串引号和注释字符 。 每个角色都可以有零个或多个这些属性。
另外,一个实例有四个标志。 这些标志表示:
- 线路终端器是否作为令牌返回或被视为仅分隔令牌的空白区域。
- C风格的评论是否被识别和跳过。
- C++风格的评论是否被识别和跳过。
- 标识符的字符是否转换为小写。
一个典型的应用程序首先构造一个这个类的一个实例,设置语法表,然后在循环的每个迭代中重复循环调用nextToken方法,直到它返回值为TT_EOF 。
测试代码:
import java.io.*;
import java.util.*;
public class Main{
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static void main(String[] args) {
int C = nextInt(), n = nextInt();
long v[] = new long[n + 10], w[] = new long[n + 10],dp[][] = new long[(1<<n) + 1][2];
int map[] = new int[(1<<n) + 10];
for(int i = 1;i<=n;++i)
v[i] = nextInt();
for(int i = 1;i<=n;++i)
w[i] = nextInt();
for(int i = 1;i<=n + 1;++i) map[1<<(i - 1)] = i;
long rs = 0;
for(int i = 1;i < (1 << n);++i) {
int lob = map[i&-i], last = i - (i & -i);
dp[i][0] = dp[last][0] + v[lob];
dp[i][1] = dp[last][1] + w[lob];
if(dp[i][0] <= C) rs = Math.max(rs, dp[i][1]);
}
System.out.println(rs);
out.flush();
}
static int nextInt() {
try {
st.nextToken();
} catch (IOException e) {
e.printstacktrace();
}
return (int)st.nval; // 如果当前令牌是一个数字,则此字段包含该数字的值。
}
static String next() {
try {
st.nextToken(); // 如果当前令牌是一个单词标记,则该字段包含一个字符串,给出单词令牌的字符。
} catch (IOException e) {
e.printstacktrace();
}
return st.sval;
}
}
感谢!
html5 – 在HTML 5 2d Canvas上下文中查找曲线上的点
我的目的是在曲线的中点绘制一个物体.使用SVG DOM,我可以使用方法getPointAtLength& getTotalLength,但我看不到HTML画布中的等价物.
解决方法
HTML画布中没有等效的东西.你必须用简单的旧数学自己找到中点.
我做了一个如何为你找到贝塞尔曲线中点的例子.在jsfiddle here上看到它.下面粘贴了一个javascript的副本.
实曲线为红色,中点为微小的绿色矩形.其他一切只是一种视觉辅助.
var ctx = $("#test")[0].getContext("2d") function mid(a,b) { return (a+b) / 2; } var cp1x = 100; var cp1y = 150; var cp2x = 175; var cp2y = 175; var x = 200; var y = 0; ctx.linewidth = 4; ctx.stroke; ctx.fill; ctx.beginPath(); ctx.moveto(0,0); ctx.bezierCurveto(cp1x,cp1y,cp2x,cp2y,x,y); ctx.stroke(); //line goes from start to control point 1 ctx.stroke; ctx.beginPath(); ctx.moveto(0,0); ctx.lineto(cp1x,cp1y); ctx.stroke(); //line goes from end to control point 2 ctx.beginPath(); ctx.moveto(x,y); ctx.lineto(cp2x,cp2y); ctx.stroke(); //line goes from control point to control point ctx.stroke; ctx.beginPath(); ctx.moveto(cp1x,cp1y); ctx.lineto(cp2x,cp2y); ctx.stroke(); // Now find the midpoint of each of those 3 lines var ax = mid(cp1x,0); var bx = mid(cp2x,x) var cx = mid(cp1x,cp2x) var ay = mid(cp1y,0) var by = mid(cp2y,y) var cy = mid(cp1y,cp2y) // draw midpoints for visual aid // not gonna look exact 'cause square // will be drawn from top-right instead of center ctx.fillRect(ax,ay,4,4); ctx.fillRect(bx,by,4); ctx.fillRect(cx,cy,4); //Now draw lines between those three points. These are green ctx.stroke; ctx.beginPath(); ctx.moveto(ax,ay); ctx.lineto(cx,cy); ctx.stroke(); ctx.beginPath(); ctx.moveto(bx,by); ctx.lineto(cx,cy); ctx.stroke(); //Now the midpoint of the green lines: // so g1 and g2 are the green line midpoints var g1x = mid(ax,cx); var g2x = mid(bx,cx); var g1y = mid(ay,cy); var g2y = mid(by,cy); //draw them to make sure: ctx.fillRect(g1x,g1y,4); ctx.fillRect(g2x,g2y,4); //Now one final line,in gray ctx.stroke; ctx.beginPath(); ctx.moveto(g1x,g1y); ctx.lineto(g2x,g2y); ctx.stroke(); //whew! We made it! var FinallyTheMidpointx = mid(g1x,g2x); var FinallyTheMidpointy = mid(g1y,g2y); //draw something at the midpoint to celebrate ctx.fill; ctx.fillRect(FinallyTheMidpointx,FinallyTheMidpointy,4);
java – 在列表中查找唯一值的快速方法
以下所有产生可接受的结果. u1似乎比预期的列表大小(约1000-2000 KVP)最快
我们能做得更好(更快)吗?
private static Set<String> u1(List<_KVPair> pairs) { Set<String> undefined = new HashSet<String>(); for (_KVPair pair : pairs) { undefined.add(pair.getValue()); } if (undefined.size() == 1) { return new HashSet<String>(); } return undefined; } private static List<String> u2(List<_KVPair> pairs) { List<String> undefined = new ArrayList<String>(); for (_KVPair pair : pairs) { if (!undefined.contains(pair.getValue())) { undefined.add(pair.getValue()); } } return undefined; } private static List<String> u3(List<_KVPair> pairs) { List<String> undefined = new LinkedList<String>(); Iterator<_KVPair> it = pairs.iterator(); while (it.hasNext()) { String value = it.next().getValue(); if (!undefined.contains(value)) { undefined.add(value); } } return undefined; }
大约3600对,’u3’获胜.大约1500对,’u1’获胜
解决方法
Set<String> undefined = new HashSet<String>(pairs.size(),1);
请注意,我使用1作为加载因子来防止任何大小调整.
出于好奇,我进行了测试(下面的代码) – 结果是(编译后):
测试1(注意:热身需要几分钟)
size of original list = 3,000 with no duplicates:
set: 8
arraylist: 668
linkedlist: 1166
测试2
size of original list = 30,000 – all strings identical:
set: 25
arraylist: 11
linkelist: 13
那种有道理:
>当有许多重复项时,List#contains将运行得相当快,因为可以更快地找到重复项并且分配大型集合哈希算法的成本正在受到影响
>当没有或很少重复时,该集合大幅度地获胜.
public class TestPerf { private static int NUM_RUN; private static Random r = new Random(System.currentTimeMillis()); private static boolean random = false; //toggle to false for no duplicates in original list public static void main(String[] args) { List<String> list = new ArrayList<>(); for (int i = 0; i < 30_000; i++) { list.add(getRandomString()); } //warm up for (int i = 0; i < 10_000; i++) { method1(list); method2(list); method3(list); } NUM_RUN = 100; long sum = 0; long start = System.nanoTime(); for (int i = 0; i < NUM_RUN; i++) { sum += method1(list); } long end = System.nanoTime(); System.out.println("set: " + (end - start) / 1000000); sum = 0; start = System.nanoTime(); for (int i = 0; i < NUM_RUN; i++) { sum += method2(list); } end = System.nanoTime(); System.out.println("arraylist: " + (end - start) / 1000000); sum = 0; start = System.nanoTime(); for (int i = 0; i < NUM_RUN; i++) { sum += method3(list); } end = System.nanoTime(); System.out.println("linkelist: " + (end - start) / 1000000); System.out.println(sum); } private static int method1(final List<String> list) { Set<String> set = new HashSet<>(list.size(),1); for (String s : list) { set.add(s); } return set.size(); } private static int method2(final List<String> list) { List<String> undefined = new ArrayList<>(); for (String s : list) { if (!undefined.contains(s)) { undefined.add(s); } } return undefined.size(); } private static int method3(final List<String> list) { List<String> undefined = new LinkedList<>(); Iterator<String> it = list.iterator(); while (it.hasNext()) { String value = it.next(); if (!undefined.contains(value)) { undefined.add(value); } } return undefined.size(); } private static String getRandomString() { if (!random) { return "skdjhflkjrglajhsdkhkjqwhkdjahkshd"; } int size = r.nextInt(100); StringBuilder sb = new StringBuilder(); for (int i = 0; i < size; i++) { char c = (char) ('a' + r.nextInt(27)); sb.append(c); } System.out.println(sb); return sb.toString(); } }
Java-将数组值分配给各个变量的快速方法
我有一种方法,split(str, ":", 2)
确切地说,它将在数组中返回两个字符串。
java中是否有比将数组中的两个值分配给字符串变量更快的方法?
String[] strings = str.split(":", 2);String string1 = strings[0];String string2 = strings[1];
例如是否有类似的语法
String<string1, string2> = str.split(":", 2);
提前致谢。
答案1
小编典典不,Java中没有这样的语法。
但是,其他一些语言也具有这种语法。示例包括Python的元组解包,以及许多功能语言中的模式匹配。例如,在Python中,您可以编写
string1, string2 = text.split('':'', 2) # Use string1 and string2
或在F#中,您可以编写
match text.Split([| '':'' |], 2) with | [string1, string2] -> (* Some code that uses string1 and string2 *) | _ -> (* Throw an exception or otherwise handle the case of text having no colon *)
关于在HTML和Java中查找值的快速方法的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于ACM必备的快速输入输出(Java)、html5 – 在HTML 5 2d Canvas上下文中查找曲线上的点、java – 在列表中查找唯一值的快速方法、Java-将数组值分配给各个变量的快速方法的相关信息,请在本站寻找。
本文标签: