JAVA Help with the ByColumn method! import java.util.*; @SuppressWarnings(\"rawt
ID: 3863854 • Letter: J
Question
JAVA
Help with the ByColumn method!
import java.util.*;
@SuppressWarnings("rawtypes")
public class Table implements Iterable<Comparable[]> {
private Comparable data[][];
// Creates a table that is a copy of the given input array
public Table(Comparable data[][]) {
int rows = data.length;
int cols = 0;
if (rows > 0) cols = data[0].length;
this.data = new Comparable[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
this.data[i][j] = data[i][j];
}
}
}
// Comparator class for comparing with respect to a single column
public static class ByColumn implements Comparator<Comparable[]> {
public ByColumn(int col, boolean ascending) {
}
Explanation / Answer
Here is the code for you:
import java.util.*;
@SuppressWarnings("rawtypes")
public class Table implements Iterable<Comparable[]> {
private Comparable data[][];
// Creates a Table that is a copy of the given input array
public Table(Comparable data[][]) {
int rows = data.length;
int cols = 0;
if (rows > 0) cols = data[0].length;
this.data = new Comparable[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
this.data[i][j] = data[i][j];
}
}
}
}
// Comparator class for comparing with respect to a single column
public static class ByColumn implements Comparator<Comparable[]> {
public ByColumn(int col, boolean ascending) {
ascending = true;
for(int i = 0; i < data.length-1; i++) //For each row.
if(data[i] >= data[i+1])
//If the elements in specified col are not in ascending order, set ascending to false, otherwise set to true.
ascending = false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.