我在java中的两个不同的类中有一个字符串数组。
当我从任何数组输入一个值时,我想得到该数组值所属的类。
那么如何通过输入数组值来了解该类?
例如:
import java.io.*;
class Car {
public static void main(String args[]) throws Exception {
System.out.println("The parts of a car are as follows");
for (int i = 1; i <= 5; i++) {
System.out.println(i + str[i]);
}
for (int j = 1; j <= 5; j++) {
System.out.println(j + ch[j]);
}
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Choose and enter any part name to group it under following categories:" + "\n" + "Engine" + "\t" + "\t" + "Bonet");
String part = dis.readLine();
if (part == ch[]) {
System.out.println("Your choosen part is " + part + " and it comes under Engine category");
} else {
System.out.println("Your choosen part is " + part + " and it comes under Bonet category");
}
}
}
class Engine {
String ch[] = {"asd", "fgh"};
}
class Bonet {
String str[] = {"qwe", "rty"};
}
现在当用户输入asd时我想显示它属于哪个类
答案 0 :(得分:3)
我不会给你完整的代码,因为我相信自己创造它会对你更好。相反,这里有一些你需要考虑的事实:
要访问存储在其他类中的数组,您必须创建该类的实例
Engine engine = new Engine();
engine.ch[0];
或者在您的情况下,您应该制作数组static
class Engine {
static String ch[] = { "asd", "fgh" };
}
并通过班级名称Engine.ch[0]
数组从0
索引到arraySize-1
要获得数组的大小,您可以使用其归档的length
,稍后再使用它
for(int i=0; i<Bonet.str.length; i++){
System.out.println(i+Bonet.str[i]);
}
readLine()
的 DataInputStream
已被删除。相反,您可以使用nextLine
java.util.Scanner
Scanner scanner = new Scanner(System.in);
//...
String part = scanner.nextLine();
要检查某个对象是否存储在数组中,您必须遍历该数组的所有元素并将它们与您的对象进行比较。另请记住,要比较String对象,您应该使用equals
之类的part.equals(otherString)
方法。
但是为了使用更少的代码,您可以将数组包装到List
并使用其contains(Object o)
方法。要将数组包装到列表中,您可以使用asList
类中的java.util.Arrays
方法。
if(Arrays.asList(Engine.ch).contains(part)){...
答案 1 :(得分:2)
将此方法付诸实施的最小变化如下。要点:
如果循环中的数组不对数组大小进行硬编码,请改用.length
import java.io.*;
public class Car {
public static void main(String args[]) throws Exception {
System.out.println("The parts of a car are as follows");
Engine engine=new Engine(); //we must create any components we have
Bonet bonet=new Bonet(); //we must create any components we have
for (int i = 0; i <bonet.str.length; i++) {
System.out.println(i +":"+ bonet.str[i]);
}
for (int j = 0; j < engine.ch.length; j++) {
System.out.println(j +":"+ engine.ch[j]);
}
DataInputStream dis = new DataInputStream(System.in);
System.out.println("Choose and enter any part name to group it under following categories:" + "\n" + "Engine" + "\t" + "\t" + "Bonet");
String part = dis.readLine();
boolean isInEngine=false; //assume isn't in engine, try to prove otherwise
for(int i=0;i<engine.ch.length;i++){
if (engine.ch[i].equals(part)){
isInEngine=true;
}
}
if (isInEngine==true) {
System.out.println("Your choosen part is " + part + " and it comes under Engine category");
} else {
System.out.println("Your choosen part is " + part + " and it comes under Bonet category");
}
}
}
class Engine {
String ch[] = {"asd", "fgh"};
}
class Bonet {
String str[] = {"qwe", "rty"};
}
注意;这远非一个最佳解决方案,需要考虑的想法:
.testPart(String string)
会更好,它会返回一个布尔值,如果它包含部分DataInputStream
(请注意,它在大多数IDE中都显示为点击),请考虑使用Scanner scanner = new Scanner(System.in);
,然后使用scanner.nextLine();
获取该行.testPart(String string)
和getName()
方法;数组/ arraylist将被声明为包含接口/ abstract-base-class Car car=new Car();
来创建Car实例,因此Car类可以使用car.printOptions();
和car.testComponent(String testString);
等方法。你正在做的方式(一个长的主要功能)将适用于小程序,但你的程序越大,就越难以像这样工作。在这种情况下,发动机和机器人将是汽车类的领域(逻辑上比他们只是'闲逛'更有意义)