![](/user_photo/_userpic.png)
9-Lekciya
.pdf![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ11x1.jpg)
Mısallar
4 elementten turıwshı pútin massiv berilgen. Massivtiń eń úlken
elementin tabıń
for hám if operatorı járdeminde
int[] mas = {25, 7, 56, 3}; int maxEl = mas[0];
for (int i = 1; i < mas.length; i++) if (maxEl < mas[i])
maxEl = mas[i]; System.out.println(maxEl); // 56
for-each operatorı hám max metodı járdeminde
int[] mas = {25, 7, 56, 3}; int maxEl = mas[0];
for (int el: mas)
maxEl = Math.max(el, maxEl);
System.out.println(maxEl); // 56
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ12x1.jpg)
Bir ólshemli massivlerdi metodlarǵa uzatıw
Ápiwayi (primitiv) tipler metodlarǵa uzatılǵanınday, massivlerdi de metodlarǵa uzatıwǵa boladı. Mısalı, tómendegi metod, massiv elementlerin nátiyjege shıǵaradı.
public static void massivShigariw(int[] a){ for(int i = 0; i < a.length; i++){
System.out.println(a[i] + " ");
}
}
Joqarıdaǵı metod tómendegishe shaqırıladı:
//massiv jaratıw hám inicializaciyalaw int[] a = new int[]{5,3,2,8,1,8};
//metodtı shaqırıw hám a massivin uzatıw massivShigariw(a);
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ13x1.jpg)
Bir ólshemli massivlerdi metodlarǵa uzatıw
Massivlerdi metodlarǵa uzatıw, primitiv tiplerden parıq qıladı. Yaǵnıy, primitiv tipler metodlarǵa uzatılǵanda, metodqa argument mánisi uzatıladı. Al massivlerde bolsa, argument jaylqasqan orınnıń siltemesi uzatıladı. Eger metod ishinde massiv mánisi ózgertilse, onda massiv
shaqırılǵan metod ishinde de massiv mánisi ózgeredi. Tómendegi |
||
mısaldı qarayıq: |
|
|
|
|
|
public class Test1 { |
|
run: |
|
|
|
public static void main(String[] args) { |
|
x=1 |
|
|
|
int x = 1; |
|
y[0]=2021 |
|
|
|
int[] y = new int[]{1}; |
|
|
manisOzgertiw(x, y); |
|
|
System.out.println("x=" + x); |
|
|
System.out.println("y[0]=" + y[0]); |
|
|
} |
|
|
} |
|
|
|
|
|
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ14x1.jpg)
Bir ólshemli massivlerdi metodlarǵa uzatıw
public static void manisOzgertiw(int a, int[] b){ a = 2021;
b[0] = 2021;
}
Bunda manisOzgertiw metodı shaqırılǵanda, x tiń mánisi a ǵa, y massiviniń siltemesi b ǵa uzatıladı. Bunda y hám b birdey siltemeden ibarat bolıp qaladı. Bunı tómendegi sxemadan kóriwge boladı:
|
Stek |
|
|
Dinamikalıq yad |
|||
|
|
|
|
||||
|
manisOzgertiw metodı |
|
|
|
|
||
|
int[] y; |
silteme |
|
|
|
|
|
|
int a:1; |
|
|
|
|
|
int tipindegi massiv |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main metodı |
|
|
|
elementleri |
||
|
|
|
|
saqlanatuǵın orın |
|||
|
int[] y; |
|
|
|
|
|
|
|
silteme |
|
|
|
|
||
|
|
|
|
|
|
||
|
int x:1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ15x1.jpg)
Bir ólshemli massivlerdi metodlarǵa uzatıw
Primitiv tiptiń mánisin hám massivlerdiń siltemelerin metodlarǵa uzatıwdıń parqın túsiniw ushın basqa mısaldı kórip shıǵayıq.
public class Test2 {
public static void main(String[] args) { int[] a = {1, 2};
//almastiriw metodı
System.out.print("almastiriw metodı shaqırılmastan aldın: ");
System.out.println("a={"+a[0]+","+a[1]+"}"); almastiriw(a[0],a[1]);
System.out.print("almastiriw metodı shaqırılǵannan keyin: ");
System.out.println("a={"+a[0]+","+a[1]+"}");
//almastiriwMassiv metodı
System.out.print("almastiriwMassiv metodı shaqırılmastan aldın: ");
System.out.println("a={"+a[0]+","+a[1]+"}"); almastiriwMassiv(a);
System.out.print("almastiriwMassiv metodı shaqırılǵannan keyin: ");
System.out.println("a={"+a[0]+","+a[1]+"}");
}
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ16x1.jpg)
Bir ólshemli massivlerdi metodlarǵa uzatıw
public static void almastiriw(int n1, int n2){ int t = n1;
n1 = n2;
n2 = t;
}
public static void almastiriwMassiv(int[] mas){ int t = mas[0];
mas[0] = mas[1]; mas[1] = t;
}
}
run:
almastiriw metodı shaqırılmastan aldın: a={1,2} almastiriw metodı shaqırılǵannan keyin: a={1,2} almastiriwMassiv metodı shaqırılmastan aldın: a={1,2} almastiriwMassiv metodı shaqırılǵannan keyin: a={2,1}
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ17x1.jpg)
Metodlardan massivlerdi qaytarıw
Massivlardı metodlarǵa uzatıwdı joqarıda kórip shıqtıq. Sonday-aq, metodlardan massivlerdi qaytarıwda múmkin. Tómendegi mısalda massiv elementlerin kerisinshe aylandırıwshı metod jaratılǵan hám bul metod massiv elementlerin keri tártipte jaylastırıp, metod shaqırılǵan orınǵa qaytaradı.
public class Test3 {
public static void main(String[] args) { int[] massiv1 = {1,2,3,4,5};
int[] massiv2 = kerisinshe(massiv1); System.out.println(Arrays.toString(massiv2));
}
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ18x1.jpg)
Metodlardan massivlerdi qaytarıw
public static int[] kerisinshe(int[] mas){ int[] keriMassiv = new int[mas.length];
for (int i = 0; i < keriMassiv.length; i++) { keriMassiv[mas.length-i-1] = mas[i];
}
return keriMassiv;
}
}
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
![](/html/88162/2422/html_fiT5u7GstG.6DS3/htmlconvd-Q5IQRJ19x1.jpg)
Tákirarlaw sorawları
Massiv degenimiz ne?
Javada massivler qalay jaratıladı?
Massiv indeksi degen ne?
Massiv jaratılǵannan soń uzınlıǵın ózgertiwge boladı ma?
Massiv elementleri arasında pútin, haqıyqıy sanlar hám qatarlar bolıwı múmkin be?