Arrays and ArrayLists can be of good use when you want to find a good configuration and you let the brick get the best answer, by simply trying out all the possible combinations stored in the Array!
The code of this ArrayList class can be found here:
http://www.koders.com/java/fid9EB794717148396BA1384B9B134F7C37FB77C880.aspx#L131
this helps understanding the things that doesn't seem to work.
for instance:
Object myArray = someArraList.toArray() ;// does not work...because it isn't in this special class (Eclipse gives it as a choice though! To let the prog crash in the brick...)
also
someArrayList.add(0, 7); //does not work, it does not even compile (the int is not recognized as object, ( but this could be official JAVA, although the comment is that the lejos Int class lacks some method...)
so what to do about that? This: make an integer object yourself!
Integer myInt = new Integer(55);
someArrayList.add(0, myInt ) ; //this works and compiles
someArrayList.add(0, "aString" ); //is accepted to, strange the int and the string are not treated the same simple way....
Now say you have your own object: (constructor in the example, nothing exceptional, haha)
integerObject someInt = new integerObject(55); //whatever this is....
then
someArrayList.add(0, someInt ); //works fine
ok so lets make it into an complete though not too functional example prog for the NXT:
import java.util.*;
import lejos.nxt.*;
public class arraytest {
static ArrayList
public static void main(String[] args) throws Exception {
integerObject someInt = new integerObject(55); //my own integer object see class below
integerObject otherInt = new integerObject(); //another one
integerObject[] rij = new integerObject[5]; //making this into an Array
rij[0] = someInt;
otherInt = rij[0]; //this works and compiles
int helloInt = (int) otherInt.ReadmyInteger (); //doing some things
LCD.drawInt( helloInt , 0 , 7 );
LCD.drawInt( (int) rij[0].ReadmyInteger () , 5 , 7 );
int myTest[] = new int [5];
myTest[0]=5;
myTest[1]=4;
myTest[2]=3;
myTest[3]=2;
myTest[4]=1;
ArrayList someArrayList= new ArrayList ();
//someArrayList.add( 3, "hello" );//this compiles
int mySize = someArrayList.size();
//+++++++++ adding strings
//someArrayList.add(0, "hello" );//this compiles and works
//++++++++++ my own objects
//someArrayList.add(0, new integerObject(55) ) ; //this works and compiles
//otherInt = (integerObject) someArrayList.get(0); //this compiles and works
Integer myInt = new Integer(55);
someArrayList.add(0, myInt ) ; //this works and compiles
myInt = (Integer) someArrayList.get(0);
mySize = someArrayList.size();
someArrayList.add(0, myInt );
someArrayList.add(0, myInt );
someArrayList.add(0, otherInt );
LCD.drawInt( myMotorAL.size() , 8 , 7 ); //and this gives the size
while(!Button.ESCAPE.isPressed() ) { //ok this keeps going on till the button is pressed
for(int teller=0;teller<>
LCD.drawInt( myTest[teller] , 0 , teller ); //it displays the content of the int array
LCD.refresh();
}
}
//here the simple class integerObject
public class integerObject {
private int myInt;
public integerObject (int comingIn ){
myInt = comingIn;
}
public integerObject ( ){
myInt = 0;
}
public int ReadmyInteger ( ){
return myInt;
}
}
ok , only to show the use of the Arrays of int and string and the ArrayList.
0 comments:
Post a Comment