[英]how to reach values in two dimensional array
public class A6{
public static void main(String[] args){
//String personInfo[][]=new String[][] {
String personInfo[][]={
{"Anderson", "Varejao", "1125"},
{"Giorgi", "Tevzadze", "1125"},
{"Will", "Cherry", "1225"},
{"Will", "Iams", "12245"},
{"Lebron", "James", "6025"},
{"Kevin", "Love", "2525"},
{"Kyrie", "Livings", "454"},
{"Kyrie", "Botti", "4544"},
{"Chris", "Mauer", "425"},
{"Mot", "Daniel", "125"},
{"Viktor", "Muller", "145"},
{"Kiamran", "Chris", "1405"},
{"Zenia", "Vaitehovic", "1025"},
{"Marija", "Grabauskaite","1471"},
{"Milda", "Grabauskaite", "1000"},
{"Dion", "Waiters", "625" },
{"Dion", "Malborg", "6250" }
};
System.out.println("Peoples on the list:");
for(int i = 0; i < personInfo.length; i++) {
for(int j = 0; j < personInfo[i].length; j++) {
System.out.print(" ");
System.out.print(personInfo[i][j]);
}
System.out.println();
}
System.out.println("----------------------");
//---------v XXX - how many people have the first name XXX----------------
String b = "v";
if(args[0].equals(b))
{
System.out.println("Persons with entered name:");
for(int i = 0; i < personInfo.length; i++) {
for(int j = 0; j < personInfo[i].length; j++) {
if(personInfo[i][j].equals(args[1]))
{
System.out.println(args[1]);
}
}
}
System.out.println();
}
//------vp XXX YYY - what is the salary of the person XXX YYY------
}
}
I started to do new self control exercise.我开始做新的自我控制练习。 Here I have two dimensional String array which holds this information {"name", "surname", "salary"}.这里我有二维字符串数组,其中包含此信息 {"name", "surname", "salary"}。 I already done one part of my exercise which counts how many people has the name that I entered into command line.我已经完成了一部分练习,它计算有多少人拥有我在命令行中输入的名字。 Now I need to write a code for example: I enter into the command line Anderson Varejao and want to get result 1125. In other words I enter a name and surname and want to get a salary of certain person from personInfo String array.现在我需要编写一个代码,例如:我进入命令行 Anderson Varejao 并希望得到结果 1125。换句话说,我输入一个名字和姓氏,并希望从 personInfo String 数组中获取某个人的薪水。 How can I reach and take a salary of person?我怎样才能达到并拿走人的工资?
for(int i = 0; i < personInfo.length; i++) {
if(personInfo[i][0].equals(args[1]) &&
personInfo[i][1].equals(args[2]) {
System.out.println(personInfo[i][2]);
}
}
System.out.println();
In other words I enter a name and surname and want to get a salary of certain person from personInfo String array.换句话说,我输入一个名字和姓氏,并想从 personInfo String 数组中获取某个人的薪水。 How can I reach and take a salary of person?我怎样才能达到并拿走人的工资?
name is stored in the first index and salary in the third index of the second dimension. name 存储在第一个索引中,salary 存储在第二个维度的第三个索引中。
So you could define a findSalary()
method that rely on that :所以你可以定义一个依赖它的findSalary()
方法:
public String findSalary(String personInfo[][], String name)
for(int i = 0; i < personInfo.length; i++) {
if (name.equals(personInfo[i][0])){
return personInfo[i][2];
}
}
return null;
}
And invoke :并调用:
String personInfo[][]={ ...};
String salary = findSalary(personInfo, "john");
Note that using String
to represent numeric things is probably not the best thing to do.请注意,使用String
表示数字事物可能不是最好的做法。
Split your search criteria by space and store result in array eg:result[2]按空间拆分搜索条件并将结果存储在数组中,例如:result[2]
check a[i][j]=result[0]firstpart && checka[i][j+1]=result[1] second half then print the expected value检查 a[i][j]=result[0]firstpart && checka[i][j+1]=result[1] 后半部分然后打印期望值
If you still want to use this way so here what you want如果您仍然想使用这种方式,那么这里是您想要的
for (int i = 0; i < personInfo.length; i++) {
if(personInfo[i][0] == args[0] && personInfo[i][1] == args[1]) {
System.out.println(personInfo[i][2]);
}
}
NOTE enter name and surname separated by spaces.注意输入以空格分隔的姓名和姓氏。
But if you want a good solution you should use the power of Object Oriented so make a class which can hold person information like :但是如果你想要一个好的解决方案,你应该使用面向对象的强大功能,所以创建一个可以保存个人信息的类,例如:
class Person {
String name = "";
String surName = "";
String salary = "";
//setters and getters here
}
With how you positioned your elements in the array, since the salary is always in personInfo[2], I'll just iterate the array one row at a time and check if the firstName corresponds to the element in peopleinfo[0] && lName corresponds to peopleinfo[1].随着你如何在数组中定位你的元素,因为薪水总是在 personInfo[2] 中,我只会一次迭代数组并检查 firstName 是否对应于 peopleinfo[0] && lName 中的元素到人信息[1]。 Looks something like this:看起来像这样:
static Scanner sc = new Scanner(System.in);
static String personInfo[][] = {
{"Anderson", "Varejao", "1125"},
{"Giorgi", "Tevzadze", "1125"},
{"Will", "Cherry", "1225"},
{"Will", "Iams", "12245"},
{"Lebron", "James", "6025"},
{"Kevin", "Love", "2525"},
{"Kyrie", "Livings", "454"},
{"Kyrie", "Botti", "4544"},
{"Chris", "Mauer", "425"},
{"Mot", "Daniel", "125"},
{"Viktor", "Muller", "145"},
{"Kiamran", "Chris", "1405"},
{"Zenia", "Vaitehovic", "1025"},
{"Marija", "Grabauskaite", "1471"},
{"Milda", "Grabauskaite", "1000"},
{"Dion", "Waiters", "625"},
{"Dion", "Malborg", "6250"}
};
public static void main(String[] args) {
System.out.print("Enter First Name: ");
String fName = sc.nextLine();
System.out.print("Enter Last Name: ");
String lName = sc.nextLine();
System.out.println(fName + " " + lName + "' Salary: " + salaryLookUp(personInfo, fName, lName));
}
static String salaryLookUp(String[][] array, String fName, String lName) {
String salary = "";
for (String[] row : personInfo) {
if (row[0].equalsIgnoreCase(fName) && row[1].equalsIgnoreCase(lName)) {
salary = row[2];
break;
} else {
salary = "Not Found";
}
}
return salary;
}
This is wrote in C# , Hope it is helpfull...这是用 C# 编写的,希望对您有所帮助...
string[,] personInfo = new string[,] {
{"Anderson", "Varejao", "1125"},
{"Giorgi", "Tevzadze", "1125"},
{"Will", "Cherry", "1225"},
{"Will", "Iams", "12245"},
{"Lebron", "James", "6025"},
{"Kevin", "Love", "2525"},
{"Kyrie", "Livings", "454"},
{"Kyrie", "Botti", "4544"},
{"Chris", "Mauer", "425"},
{"Mot", "Daniel", "125"},
{"Viktor", "Muller", "145"},
{"Kiamran", "Chris", "1405"},
{"Zenia", "Vaitehovic", "1025"},
{"Marija", "Grabauskaite","1471"},
{"Milda", "Grabauskaite", "1000"},
{"Dion", "Waiters", "625" },
{"Dion", "Malborg", "6250" }
};
string FirstName = "Milda";
string LastName = "Grabauskaite";
for (int i = 0; i < personInfo.GetLength(0); i++)
{
if(personInfo[i,0] == FirstName && personInfo[i,1] == LastName)
{
Console.WriteLine(FirstName + " " + LastName + " Salary Is: " + personInfo[i,2]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.