0.1 ESEGUIRE UN COMANDO DI SHELL String cmd = "ls -al"; Runtime run = Runtime.getRuntime(); Process pr = run.exec(cmd); pr.waitFor(); BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line = ""; while ((line=buf.readLine())!=null) { System.out.println(line); }
0.2 CONFRONTARE DUE OGGETTIpublic String[] sortNodes(ArrayList<Node> nodes) { Node[] sortedNodes = new Node[nodes.size()]; Collections.sort(nodes, new Comparator<Node>() { public int compare(Node o1, Node o2) { return o2.priority - o1.priority; } }); for (int i=0; i < nodes.size(); i++) { sortedNodes [i] = nodes.get(i); } return sortedNodes ; } class Node{ public String name = null; public int priority; }
0.3 CENTRARE UN TESTO IN SWINGString s; int width, height; Graphics g; FontMetrics fm = getFontMetrics(ftDefault); Rectangle2D textsize = fm.getStringBounds(s, g); int xPos = (width - textsize.getWidth()) / 2; int yPos = (height - textsize.getHeight()) / 2 + fm.getAscent(); g.drawString(s, xPos, yPos);
0.4
TROVARE UNA SOTTOSTRINGAclass ContinueWithLabelDemo { public static void main(String[] args) { String searchMe = "Look for a substring in me"; String substring = "sub"; boolean foundIt = false; int max = searchMe.length() - substring.length(); test: for (int i = 0; i <= max; i++) { int n = substring.length(); int j = i; int k = 0; while (n-- != 0) { if (searchMe.charAt(j++) != substring.charAt(k++)) { continue test; } } foundIt = true; break test; } System.out.println(foundIt ? "Found it" : "Didn't find it"); } }
Video-Corsi
Musica
Grafica
3D
 |