0

help... - java

question/task: https://edabit.com/challenge/Md6usCHQ7Xsj2fQi3 how can i make it work? my code: import java.util.*; public class Main { public static void main(String[] args) { Integer[] arr = {3,2,1}; order(arr); } public static void order(Integer[] arr){ boolean isGood = true; List<Integer> list = Arrays.asList(arr); Collections.sort(list); for (int i = 0; i < list.size()-1; i++){ for (int j = list.get(0); j <= list.size(); j++){ if (list.get(i) != j){ isGood = false; } } } System.out.println(isGood); } }

2nd Dec 2020, 5:00 PM
Yahel
Yahel - avatar
7 ответов
+ 2
Yahel there in loop, we have 3 parts for(initialization part ; loop condition ; increment/decrecents ) { ..//body } This is the structure, you can use it as we needed.. No limitation of assignments in 1st block but only allows assignments.., But you should have only 1 condition ; and also no limits in 3rd part, there you can added any proper instructions with comma separations.. Ex : check this code without any loop body, you get output.... for( int i=0,j=0,k=0 ; i<20 && j<10 ; i=i+2, j++, System.out.print(k--)) { //loop body, now empty }
2nd Dec 2020, 10:41 PM
Jayakrishna 🇮🇳
+ 2
The condition : I<list.get(0) && j < list.size() becomes false 1st itself because i=0, j =4 (list[0]) so list. Size is 3. So 0<3 && 4< 3 False. No need j<list. size(), just use i<list.size() is enough...
3rd Dec 2020, 11:29 AM
Jayakrishna 🇮🇳
+ 1
No need 2 loops, Try change to this : single loop for (int j = list.get(0),i=0; j < list.size();i++, j++){ Also on false, you can break loop..
2nd Dec 2020, 8:45 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 wow, I have never seen a for loop with more than one increasing variable.. it's quite new to me. Do you have a video, article or any explanation about it?
2nd Dec 2020, 9:23 PM
Yahel
Yahel - avatar
0
Jayakrishna🇮🇳, Thanks! but whats the problem now? code: import java.util.*; public class Main { public static void main(String[] args) { Integer[] arr = {4,10,3}; order(arr); } public static void order(Integer[] arr){ boolean isGood = true; List<Integer> list = Arrays.asList(arr); Collections.sort(list); for (int i = 0, j = list.get(0); i < list.size() && j <= list.size(); i++, j++){ if (list.get(i) != j){ isGood = false; } } System.out.println(isGood); } }
3rd Dec 2020, 6:07 AM
Yahel
Yahel - avatar
0
Jayakrishna🇮🇳, Thanks! Its working great!
3rd Dec 2020, 12:07 PM
Yahel
Yahel - avatar
0
You're welcome...
3rd Dec 2020, 12:09 PM
Jayakrishna 🇮🇳