0% found this document useful (0 votes)
3 views2 pages

Java Arrays and Sorting Guide

The document provides examples of Java programming concepts, including the use of arrays, loops with break and continue statements, and sorting techniques. It demonstrates how to create and manipulate arrays, implement conditional statements, and sort objects using both natural and alternative sorting methods. Additionally, it includes code snippets for defining classes and implementing the Comparable and Comparator interfaces.

Uploaded by

hiwekij165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java Arrays and Sorting Guide

The document provides examples of Java programming concepts, including the use of arrays, loops with break and continue statements, and sorting techniques. It demonstrates how to create and manipulate arrays, implement conditional statements, and sort objects using both natural and alternative sorting methods. Additionally, it includes code snippets for defining classes and implementing the Comparable and Comparator interfaces.

Uploaded by

hiwekij165
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

python code learnifndgdsaj

fdkajfklajskfasl

fdflafdkalflaklfls

ArraysBreak / Continue
Animal [] zoo = new Animal [4];
zoo [0] = new Tiger();
zoo [1] = new Giraffe();
…for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
//This skips the value of 4
}
if (i == 6) {
break;
//This jumps out of the for loop
}
}
String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
[Link]([Link]);
//Outputs 4
int [][] myNumbers = {{1, 2, 3, 4}, {4, 5, 6}};
int x = myNumbers [1][2];
[Link](x);
//Outputs 6
If...Else
[Link](cars);
[Link]([Link](cars));
//[BMW, Ford, Mazda, Volvo]
SORTING OBJECTS WITH MULTIPLE PARAMETERS:
NATURAL SORTING
(Created within class)
public class Person implements Comparable<Person>{…
@Override
public int compareTo(Person o) {
return [Link]([Link], [Link]);
}
----> Use wrapperclass
int time = 22;
if (time < 10) {
[Link]("Good morning!");
} else if (time < 20) {
[Link]("Good day!");
} else {
[Link]("Good evening!");
}
//Outputs "Good evening!"
variable = (condition) ? expressionTrue :
[Link](listOfPeople);
/
[Link](...);
ALTERNATIVE SORTING
(Created in sepparate class)
public class SortOnName implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return [Link]().compareTo([Link]());

You might also like