package notebook; import java.util.ArrayList; public class f { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<String> list = new ArrayList<>(); list.add("Cat"); ArrayList<String> list1=list; ArrayList<String> list2=(ArrayList<String>)(list.clone()); list.add("Dog"); list1.add("Tiger"); list2.add("Fox"); System.out.println(list==list1); System.out.println(list==list2); System.out.println("list1 is "+list1); System.out.println("list2 is "+list2); System.out.println("list2’size is "+list2.size()); } } 什么意思
时间: 2024-04-11 08:30:08 浏览: 148
这段代码主要是演示了Java中ArrayList的一些操作。首先创建了一个ArrayList对象list,并向其中添加了一个元素"Cat"。然后创建了一个list1对象,并将list赋值给list1。接着创建了一个list2对象,通过调用list的clone()方法来复制list中的元素,并将复制后的元素赋值给list2。然后分别向list、list1和list2中添加了不同的元素。最后输出了一些结果,包括判断list、list1和list2是否相等,分别打印list1和list2的内容,以及list2的大小(即元素个数)。
相关问题
import java.util.*; public class Task { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList(args)); Collections.addAll(names, "John", "Kate", "Tom", "Bob", "Alice"); // 排序---------------------------------------------- /* TODO 对names集合进行排序 */ Collections.sort(names); System.out.println(names); // 打乱----------------------------------------------- Random random = new Random(names.size()); /* TODO 使用random打乱names集合 */ System.out.println(names); } }
对names集合进行排序的代码如下:
Collections.sort(names);
使用random打乱names集合的代码如下:
Collections.shuffle(names, random);
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main{ public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int n; double need; double sum=0; String s[]=in.readLine().split(" "); n=Integer.parseInt(s[0]); need=Double.parseDouble(s[1]); String amount[]=in.readLine().split(" "); String price[]=in.readLine().split(" "); ArrayList<Mooncake> cakes=new ArrayList<Mooncake>(); for(int i=0;i<n;i++){ Mooncake cake=new Mooncake(); cake.setAmount(Double.parseDouble(amount[i])); cake.setSales(Double.parseDouble(price[i])); cake.setValues(Double.parseDouble(price[i])/Double.parseDouble(amount[i])); cakes.add(cake); } Collections.sort(cakes); for(Mooncake c: cakes){ if(need>=c.amount){ need=need-c.amount; sum+=c.amount*c.values; } else{ sum+=need*c.values; need=0; } if(need==0){ break; } } System.out.printf("%.2f",sum); } static class Mooncake implements Comparable<Mooncake>{ double amount; double sales; double values; public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public double getSales() { return sales; } public void setSales(double sales) { this.sales = sales; } public double getValues() { return values; } public void setValues(double values) { this.values = values; } @Override public int compareTo(Mooncake arg0) { // TODO Auto-generated method stub return this.values>arg0.values?-1:1; } } }转c++
把Java代码转换成C++代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Mooncake {
double amount;
double sales;
double values;
bool operator<(const Mooncake& c) const {
return this->values > c.values;
}
};
int main() {
int n;
double need, sum = 0;
cin >> n >> need;
vector<Mooncake> cakes(n);
for (int i = 0; i < n; i++) {
cin >> cakes[i].amount;
}
for (int i = 0; i < n; i++) {
double price;
cin >> price;
cakes[i].sales = price;
cakes[i].values = price / cakes[i].amount;
}
sort(cakes.begin(), cakes.end());
for (const auto& c : cakes) {
if (need >= c.amount) {
need = need - c.amount;
sum += c.amount * c.values;
}
else {
sum += need * c.values;
need = 0;
}
if (need == 0) {
break;
}
}
printf("%.2f", sum);
return 0;
}
阅读全文