Java 如何将同一字符串数组排序为新字符串并对其计数。
我正在做一个java项目,我想对相同的字符串数组进行排序,然后计算每个汽车品牌有多少个。我无法将数组传递给int变量。我需要建议。谢谢Java 如何将同一字符串数组排序为新字符串并对其计数。,java,arrays,Java,Arrays,我正在做一个java项目,我想对相同的字符串数组进行排序,然后计算每个汽车品牌有多少个。我无法将数组传递给int变量。我需要建议。谢谢 public class SortCars { public static void main(String[] args) { String[] cars = {"honda", "toyota", "honda", "toyota"}; String[] honda = new String[5]; String[] toyota
public class SortCars {
public static void main(String[] args) {
String[] cars = {"honda", "toyota", "honda", "toyota"};
String[] honda = new String[5];
String[] toyota = new String[5];
int numOfHonda;
int numOfToyota;
for (int i = 0; i < cars.length; i++) {
if (cars[i].equals("numOfHonda")) { //
honda[i] = cars[i];
honda[i] = numOfHonda; // stuck here
numOfHonda++;
}
}
}
公共类分拣机{
公共静态void main(字符串[]args){
字符串[]cars={“honda”、“toyota”、“honda”、“toyota”};
字符串[]本田=新字符串[5];
字符串[]丰田=新字符串[5];
int NUMOFONDA;
国际丰田汽车公司;
对于(int i=0;i
}如果允许您使用,它将非常简单。这是密码
String[] cars = {"honda", "toyota", "honda", "toyota"};
Map<String, Long> data = Arrays.stream(cars)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(data); // => {toyota=2, honda=2}
String[]cars={“本田”、“丰田”、“本田”、“丰田”};
地图数据=数组.stream(汽车)
.collect(Collectors.groupingBy(Function.identity()、Collectors.counting());
System.out.println(数据);//=>{丰田=2,本田=2}
那么您想查看一系列汽车,并根据制造商的不同将其分类为不同的阵列?如果是这样,我的方法如下:
import java.util.ArrayList;
String[] cars = {"honda", "toyota", "honda", "toyota"};
ArrayList<String> honda = new ArrayList<String>();
ArrayList<String> toyota = new ArrayList<String>();
for (int i = 0; i < cars.length; i++) {
switch(cars[i]) {
case "honda":
honda.add(cars[i]);
break;
case "toyota":
toyota(cars[i]);
break;
}
}
import java.util.ArrayList;
字符串[]cars={“honda”、“toyota”、“honda”、“toyota”};
ArrayList honda=新的ArrayList();
ArrayList toyota=新的ArrayList();
对于(int i=0;i
我建议使用更通用的ArrayList(java.util.ArrayList)。此外,我将使用switch语句。对我来说,计算每个制造商的数量似乎没有必要,只需通过honda.length
查看即可
编辑:深入解释 ArrayList 好的,这里是一步一步。我的代码与你的代码没有太大区别。我将制造商数组更改为“CustomDatatype”。在引擎盖下,它使用了一个数组,但它为我们提供了一些功能,使我们更容易与该数组交互(例如,使用
.add()
方法轻松地将元素添加到ArrayList)。要初始化ArrayList,我们需要调用new
-关键字,因为它是一个类而不是基本数据类型。我们还需要告诉数组它里面的元素有什么数据类型,这就是我们用尖括号(ArrayList()
)
开关()
函数将“给出”的内容作为括号中的参数,并尝试将其与switch函数中定义的情况匹配。如果找到匹配项,它将运行案例定义的任何代码。Switch语句还具有fall-through的功能,即使在找到第一个匹配项时,它也会不断检查匹配项,这可能很有用,但目前使用break
-语句可以防止这种情况。一个合适的开关案例也有一个默认的
案例,当找不到匹配项时将运行该案例,但由于复杂性,这里也省略了这一点。(@otherDevs:sue me,我在解释)
结论
我试着用你的方法展示一种方法,并稍微改变一下,但总的来说是一样的,当然有很多方法可以解决这个问题,比如
Map
等等。但由于您刚刚开始,我建议您从基础开始和您确实应该熟悉Java文档,因为作为一名开发人员,您可以阅读和使用文档,这是一个纯金级的解决方案。使用TreeMap-简单而精确的解决方案
String[] cars = {"honda", "toyota", "honda", "toyota"};
TreeMap<String, Integer> map = new TreeMap<>();
for(String s: cars){
int count = map.containsKey(s) ? map.get(s) : 0;
map.put(s, count+1);
}
System.out.println(map);
您似乎混淆了字符串和int值。因为你说你正处于编程的第一周,从我对这个问题的理解来看。这就是您正在尝试做的:-
public static void main(String[] args) {
String[] cars = {"honda", "toyota", "honda", "toyota"};
String[] honda = new String[5];
String[] toyota = new String[5];
int numOfHonda = 0; // you need to initialize these int variables
int numOfToyota = 0;
for (int i = 0; i < cars.length; i++) {
if (cars[i].equals("honda")) { // if cars has element honda
honda[i] = cars[i]; // putting cars element in honda array
numOfHonda++; //incrementing value of numOfHonda by 1
}
else if(cars[i].equals("toyota")){
toyota[i] = cars[i];
numofToyota++;
}
}
if(cars[i].equals(“numofonda”)
在这里,您试图将cars[i]元素与字符串“numOfHonda”进行比较。如果您查看代码,则没有声明为“numOfHonda”的字符串。有一样东西是int型变量
equals()方法不将int类型与String进行比较。它总是比较字符串和字符串。事实上,将int变量numofHonda放在“”中并不是一回事!“Numofonda”和numHonda是不同的东西
honda[i]=numOfHonda代码>
你被困在这里是因为你知道你在这里做什么?
您正在尝试放置int类型变量(numOfHonda是int类型的,记得吗?)
在字符串数组中。这是不可能的。这是一种类型不匹配。任何类型数组的基本定义都是它包含相同类型的元素。因此,honda[]无法存储int类型值
希望你现在有更好的视野。继续学习。听起来你想要一张地图
你想要实现什么?只需计算有多少本田和丰田,或者将其分为两个系列?将其分为两个系列,然后跟踪每个系列中有多少本田或丰田。使用TreeMap。最好的解决方法:)不要把“已解决”放在你的问题标题中。如果其中一个答案回答了你的问题,那就接受它。此外,如果任何答案对你有一点点帮助,请向上投票。请查看以了解有关此站点最佳实践的更多信息。首先谢谢,然后是。我想让本田和丰田在他们自己的阵列。我已经进入编程的第一周了,所以我还不太了解。更新了我的答案,并试图解释得更详细。首先谢谢。但不确定我们是否可以使用它,我们进入了编程的第一周,所以这段代码看起来比我们所处的位置更先进。如果这不是您想要的。请随意评论!非常感谢。这消除了我的许多困惑。就像你提到的,我在尝试将字符串数组转换为int时感到困惑。我应该提到的是,我才刚刚开始学习。@Rnhep如果你不介意的话,这个答案对你来说是否足够。你能将它标记为已接受的答案吗?
public static void main(String[] args) {
String[] cars = {"honda", "toyota", "honda", "toyota"};
String[] honda = new String[5];
String[] toyota = new String[5];
int numOfHonda = 0; // you need to initialize these int variables
int numOfToyota = 0;
for (int i = 0; i < cars.length; i++) {
if (cars[i].equals("honda")) { // if cars has element honda
honda[i] = cars[i]; // putting cars element in honda array
numOfHonda++; //incrementing value of numOfHonda by 1
}
else if(cars[i].equals("toyota")){
toyota[i] = cars[i];
numofToyota++;
}
}
int numOfHonda; //initilize your variable. Otherwise you will get compilation error! make it = 0 in this case.
int numOfToyota; // same here, initialize to 0.