Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (2014) Testing and Debugging Debugging with DDMS

Dylan Merritt
Dylan Merritt
15,682 Points

I am working on a personal app of mine. I would like two arrays of Strings to match to one another.

So in my code, I have two classes, each containing an array of Strings. I use a Random selector to select an item in the array from class 1, and I would like the selection from the array in class 2 to be the same number as the Random selector selected for class 1, and the two array correspond to one another. Does anyone know how I might accomplish this or maybe a better way to do it?

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Dylan

I'm just wondering why two strings that you wish to concatenate together are located in two separate classes as it does make the process a little trickier but not impossible.

I am assuming you are trying to replicate the get fact method in another class.

I would firstly try to include the second array in the method where the first array is located. You could then do your concatenation at the end of that method and return the result which would be the preferred option.

Secondly you could declare the each of the methods as public and pass the result of the random number generator into each of the method calls. Therfore using the same number each time.

I hope you understand the above, if you need help writing the code. Please paste your code back here and I can take a look through it.

Hope this helps Daniel

Ratul Sarna
Ratul Sarna
Courses Plus Student 11,618 Points

Here is a possible solution -

1) Store the number you get from the random generator in a variable, say "i".

2) Create a method in your other class such as
```public String getAt (int index) { return array[index]; }

where array is the String[] array you have in the other class.

3) Call this method with the "i" as the argument.

I hope this answers your query. Also, it would be better to provide a little more detail about your problem. Like why exactly are you having to do so with code snippets.