Siti, blog e web hosting gratis Crea sito web
Flaviusso Sbrughenaz!

Esempi JaVa


Per eventuali errori di scrittura o per eventuali collaborazioni con me, potrete mandarmi un e-mail al indirizzo: flaviusso@email.it


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 OGGETTI

public 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 SWING

String 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 SOTTOSTRINGA

class 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
Corso

Musica
Suonerie Gratis

Grafica 3D

Sponsor

Il sito è privo di qualsiasi CopyRight© - I contenuti sono riproducibili tranquillamente, io non mi assumo le responsabilità di come queste informazioni vengono usate e da eventuali danni recati a terzi. Tutto ciò che si trova in queste pagine non è usato a scopo di lucro ma solo informativo.
Flaviusso Production