简体   繁体   中英

pass array names as parameter in class method

 // 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); } 

I have a class with many arrays named in alphabets like char [] a, char [] b etc. I also have a method in class. In main class I have created an object and if i need to pass two alphabets which will be like reference for calling only those arrays whose name are passed. like my line of code in main class is as follows: object.function(char1, char2); these characters will be alphabets(a,b,c etc) can it be done ?? how ?? please help. I searched it but exact problem is not answered.. Regards

If you are asking how to pass char arrays to a function, all you need to do is set up your function as follows:

public static void MyFunction(char[] a, char[] b) {
    //do stuff to char arrays
}

Then when you call the function, you will be able to pass them in with:

char[] a = {'a', 'b', 'c'};
char[] b = {'d', 'e', 'f'};
MyObject.MyFunction(a, b);

It would be helpful if you posted your current code so I can tell exactly what it is you're trying to do, though.

EDIT:

If you want to be able to call the arrays with a char, I'd suggest containing them in a 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.

Then you can call the arrays as follows:

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'

So a function could be something like this:

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]
    }
}

Demonstration Here

Hope this helps.

I found your question kind of confusing, so if I am way off, please tell me.

However, what I think your trying to do is is call a char array with a character. For instance calling the char array c with the character 'a'. You can do this with if conditionals of switches. Also, what does your object.function(char1, char2) actually do? That would help me out in answering you question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM