I'm new to arrayList and now I am having trouble with the output. It requires me to take the second char in String Owner compared to other and sort by descending. I tried by using a variable temp1 to swap 2 of them.
The list before run : (A8,1) (B1,2) (C7,3) (D2,4) (E6,5) (F3,6)
but my output is: (A8,1) (B1,2) (D2,4) (F3,6) (E6,5) (C7,3) (not correct)
@Override
public void f3(List<Cala> list) {
for (int i = 0; i < list.size(); i ) {
char max = list.get(i).getOwner().charAt(1);
for (int j = 1; j < list.size(); j ) {
char temp = list.get(j).getOwner().charAt(1);
if (max < temp) {
Cala temp1 = list.get(i);
list.set(i, list.get(j));
list.set(j, temp1);
}
}
}
}
Please help me what is wrong with this :< Thank you guys
This is my class Cala. I cannot access to Main class because Main's file extension is Main.class
public class Cala {
private String owner;
private int price;
public Cala(){
owner = "";
price = 0;
}
public Cala(String owner, int price) {
this.owner = owner;
this.price = price;
}
public String getOwner() {
return owner;
}
public int getPrice() {
return price;
}
public void setOwner(String owner) {
this.owner = owner;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "(" owner "," price ")" ;
}
}
CodePudding user response:
I understand that you want to sort the list of Cala
objects, by the digit in the owner
, using bubble sort algorithm. The following code does that:
import java.util.ArrayList;
import java.util.List;
public class Cala {
private String owner;
private int price;
public Cala() {
this("", 0);
}
public Cala(String owner, int price) {
this.owner = owner;
this.price = price;
}
public String getOwner() {
return owner;
}
public int getPrice() {
return price;
}
public void setOwner(String owner) {
this.owner = owner;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "(" owner "," price ")";
}
private static int getDigit(Cala cala) {
if (cala != null) {
String owner = cala.getOwner();
if (owner.length() > 1) {
char digit = owner.charAt(1);
return digit - '0';
}
else {
return 0;
}
}
else {
return 0;
}
}
public static void main(String[] args) {
List<Cala> list = new ArrayList<>();
list.add(new Cala("A8",1));
list.add(new Cala("B1",2));
list.add(new Cala("C7",3));
list.add(new Cala("D2",4));
list.add(new Cala("E6",5));
list.add(new Cala("F3",6));
System.out.println("Before: " list);
int len = list.size();
for (int i = 0; i < len - 1; i ) {
for (int j = 0; j < len - i - 1; j ) {
if (getDigit(list.get(j 1)) < getDigit(list.get(j))) {
Cala swap = list.get(j);
list.set(j, list.get(j 1));
list.set(j 1, swap);
}
}
}
System.out.println(" After: " list);
}
}
Running the above code produces the following output:
Before: [(A8,1), (B1,2), (C7,3), (D2,4), (E6,5), (F3,6)]
After: [(B1,2), (D2,4), (F3,6), (E6,5), (C7,3), (A8,1)]
CodePudding user response:
I'm not sure about the variable names and everything, but this is what I came up with
for(int i = 0; i < arrayList.size(); i ){
int temp = Integer.parseInt(arrayList.get(i).substring(1,2);
for(int j = 1; j < arrayList.size(); j ){
int temp1 = Integer.parseInt(arrayList.get(j).substring(1,2);
if(j > i){
arrayList.swap(i, j);
The main thing I would try is potentially the parseInt, this code probably has a lot of problems but that's a baseline of what I would do