// My code is doing something; difficult to get.. still a concept can be grasped.
//I am having my method (searchCity) in class graph. this method is called from main
//class and is yes... selecting one array by charachter it is passed with
public class graph {
int a = 1000;
int flag = 0;
//array of all cities having elements as connection to other cities
public graph(){
char [] i = {'i','v'};
char [] v = {'v','u'};
char [] u = {'u','b','h'};
char [] b = {'b','p','f','u'};
char [] h = {'h','u','e'};
char [] e = {'e','h'};
char [] p = {'p','b','r','c'};
char [] c = {'c','p'};
char [] r = {'r','s','p'};
char [] s = {'s','f','r'};
char [] f = {'f','s','b'};
}
public void searchCity( char i, char j){
// check for equal array as parameter i (include must )
for (int z = 0 ; z < i.length; z ++) {
if (i[z] == 'j') {
int ascii = (int) 'j';
int flag = 1;
System.out.println(ascii);
}
else {
// checking for smallest cost in the complete array
int ascii = (int) i[z];
if(a>ascii)
a=ascii;
else continue;
}
}
if (flag==0){
char b = (char) a;
char [] c = {'b'};
}
searchCity(c, j);
}
我有一个包含许多数组的类,如char [] a,char [] b等。我在课堂上也有一个方法。 在主类中,我创建了一个对象,如果我需要传递两个字母表,它们就像只调用那些名字传递的数组一样。 像我在主类中的代码行如下: object.function(char1,char2); 这些字符将是字母(a,b,c等)可以做到吗?怎么样 ??请帮忙。我搜索了它,但确切的问题没有得到解答..问候
答案 0 :(得分:2)
如果您询问如何将char数组传递给函数,您需要做的就是按如下方式设置函数:
public static void MyFunction(char[] a, char[] b) {
//do stuff to char arrays
}
然后,当您调用该函数时,您将能够使用以下命令传递它们:
char[] a = {'a', 'b', 'c'};
char[] b = {'d', 'e', 'f'};
MyObject.MyFunction(a, b);
如果您发布了当前的代码,那将会很有帮助,因此我可以确切地说出您正在尝试做什么。
修改强>
如果您希望能够使用char调用数组,我建议在HashMap中包含它们:
Map<Character, Character[]> graph = new HashMap<Character, Character[]>();
graph.put('i', new Character[] {'i', 'v'});
graph.put('v', new Character[] {'v', 'u'});
graph.put('u', new Character[] {'u', 'b', 'h'});
// etc.
然后你可以按如下方式调用数组:
System.out.println(graph.get('i')[0]); // Prints 'i'
System.out.println(graph.get('i')[1]); // Prints 'v'
System.out.println(graph.get('i').length); // Prints '2'
所以函数可能是这样的:
public static void MyFunction(char a, char b) {
graph.get(a)[0]; // grab first character in array
for (int i=0; i<graph.get(b).length; i++) {
// recursively go through array with graph.get(b)[i]
}
}
希望这有帮助。
答案 1 :(得分:0)
我发现你的问题有点令人困惑,所以如果我离开了,请告诉我。
但是,我认为你要做的是调用带字符的char数组。例如,使用字符&#39; a&#39;来调用char数组c。你可以使用if条件的开关来做到这一点。另外,你的object.function(char1,char2)实际上做了什么?那会帮助我回答你的问题。