0

// 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

3
  • share some code as well. You can try Reflection API to get the name of variable or methods. Commented Mar 6, 2015 at 19:11
  • You probably don't want to do that (when you are ready to use Reflection or other such tricks you won't need to ask about questions that may require Reflection).. however without some "actual code" it's hard to understand what the intent/problem is. Commented Mar 6, 2015 at 19:13
  • @user2864740 Edited with code Commented Mar 6, 2015 at 19:22

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

No i am not asking that... As It is really simple.. i am asking for what exactly Anthony dito explained. call a char array with a character. For instance calling the char array c with the character 'a'. Please help me with this.. i have edited my post with some code.. that might assist you. please look Edited
Thank you #Oceanity. yes it seems helpful. let me implement it. Thanks much
Not a problem! If this solution helps solve your issue be sure to mark it as the accepted answer :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.