20 lut 2010

Tablica



//pomocnicza funkcja
private void testObjectTab(Object[] obTab){
for(Object ob: obTab)
prn(ob);
}


@Test
public void Tab3Init(){
int[]test = new int[5];
for(int x:test)
prn(x);
//wynik: 0,0,0,0,0
}
// @Test
public void InitTab2Test(){

testObjectTab(new Object[]{
new Integer(1),
new Float(1.1f),
new Double(1.1)}
);
//wynik: 1 , 1.1 ,1.1
}

// @Test
public void multiTab(){
int[][] mTab ={
{1,2,3},
{4,5,6}
};

for(int[] row:mTab)
for(int cell:row)
prn(cell);
//wynik: 1,2,3,4,5,6
assertEquals(mTab.length, 2); //pass!!!!!!!!!!!
}

// @Test
public void InitTest() {
int tab[];
int[] tab2;

int tab3[] = {1, 2};
int tab4[] = {1, 2, 3, 4, 5,}; //patrz przecinek! =>Ok



int tab4copy[] = tab4;

for (int x : tab4copy) {
x++; //nie zmienia nic w tablicy!
}
for (int x = 0; x < tab4copy.length; x++) {
tab4copy[x]++;
}
//sprawdzamy czy zmieniło się w 2 tablicach ( tab4copy i tab4)
for (int x : tab4) {
prn(x); //2,3,4,5,6
}
assertEquals(tab4, tab4copy); //pass


//test inicjalizacji
//!int tab5[3]; nie wolno bebe
int tab5[] = new int[0];
prn(tab5.length); //0



int tab6[] = new int[(char) 1];
int tab7[] = new int[(byte) 1];
//! int tab7[] = new int[(long)1]; zonk max może być Z
int tab9[] = new int[(int) 1.1];
int tab10[] = new int[(char) 1];

//pozyższe się udaje bo promowanie do inta

//ciekawostka
//! float f=1.1; bo 1.1 to double!


//tablioce obiektów
String[] s = {"ok", "", new String("ok")}; //ok
// String tabS[]={"ok",tabS[0]}; //variable might be not inicjalized!
Integer[] intTab = {1, 1, 3, new Integer(1)};

//int a Integer

//!int[] tabInt= new Integer[3]; zonk
//!Integer[] tabInteger= tab4; zonk


}