Exemple :
public static void main (String args[]) {
int a=10;
toto=new double[10];
cerise();
System.out.println("toto[0] a comme valeur"+toto[0]);
}
public static void cerise() {
toto[0] = cerisier;
// pourquoi renvoyer quelque chose ?
// j'ai laissé en static meme si je n'en vois pas l'intérêt (mais je n'ai pas tout le code donc il se peut que cela soit nécessaire).
}
public static double toto[];
public static double cerisier = 200;
}
macErmite a dit:Il m'arrive quelque chose de curieux sur ces lignes de codes :
int nombre_surface = 27;
int nposition = 8;
int rAposition;
int eAposition;
int nAposition;
if ( nposition <= nombre_surface){
rAposition = nposition-1;
eAposition = nposition-2;
nAposition = nposition+1;
}
if (nposition > nombre_surface+1){
rAposition = nombre_surface;
eAposition = nombre_surface-1;
nAposition = nombre_surface+1;
}
int p=1;
int cr = rAposition; -->variable rAposition might not have been initialized
grandgibus a dit:...
if ( nposition <= nombre_surface){
rAposition = nposition-1;
eAposition = nposition-2;
nAposition = nposition+1;
} else {
rAposition = nombre_surface;
eAposition = nombre_surface-1;
nAposition = nombre_surface+1;
}
...
public int[] foo() {
int[] retValue = new int[10];
for (int i=0; i<reValue.length; i++) {
retValue = i * i;
}
return retValue;
}
public class GrafArr{
private int mWidth ;
private int mHeight ;
private int mNumCol ;
private int[] mValue ;
private int mMinValue;
private int mMaxValue ;
private int i;
public GrafArr() {
this.mWidth = 5 ;
this.mNumCol = 20 ;
this.mHeight = 100;
this.mMinValue = 0 ;
this.mMaxValue = 512 ;
//initialiser le tableau
this.mValue= new int[mNumCol];
for(i=0;i<mNumCol;i++){
mValue[i] = 0 ;
}
}
public void addValue(int theValue) {
theValue = Math.max(theValue,mMinValue) ;
/* theValue = Math.min(theValue,mMaxValue) ; */
for(i=0;i<mNumCol-1;i++) {
mValue[i] = this.mValue[i+1] ;
}
mValue[this.mNumCol-1] = theValue ;
}
public void drawGraph(Graphics g) {
for(i=0;i<this.mNumCol;i++) {
System.out.println(mValue[i]);
int theHeight = Math.round((this.mValue[i]-this.mMinValue)*this.mHeight/this.mMaxValue) ;
if (theHeight>0) {
g.setColor(Color.red) ;
g.fillRect(i*mWidth,50,mWidth,-theHeight) ;
}
/*
if (theHeight>0) {
g.setColor(Color.orange) ;
g.fillRect(i*mWidth,50,mWidth,theHeight) ;
}
*/
}
}
}
public class GraficArrayRunner extends java.applet.Applet implements Runnable{
private GrafArr graficarr;
private Thread thread;
Random rand;
public void init() {
graficarr = new GrafArr() ;
thread = new Thread(this);
thread.start();
setBackground(Color.black) ;
}
public void paint (Graphics g) {
super.paint(g);
graficarr.addValue(rand.nextInt(200)) ;
graficarr.drawGraph(g);
}
public void run(){
while (true) {
try{thread.sleep(1000);
rand = new Random();
repaint();
}catch(InterruptedException e){
System.out.println(e);
}
}
}
}
GrandGibus a dit:il suffit de renvoyer le tableau (au lieu de ses données)... Un exemple :
C'est pareil pour les objets... voire les tableaux d'objets (à noter que int n'est pas un objet, mais un type simple... contrairement à son homologue objet Integer[:i] )
public int[] foo() {
int[] retValue = new int[10];
for (int i=0; i<retValue.length; i++) {
retValue = i * i;
}
return retValue;
}
public void fooClient() {
int[] clientValue = foo();
for (int i=0; i< clientValue.length; i++) {
System.out.println("n°" + i +" = " + clientValue);
}
}