36

I have 2 String Arrays

String[] question1 = {"Q1","Q2","Q3","Q4"};
String[] question2 = {"Q5","Q6","Q7","Q8"};

And one ArrayList

ArrayList<String> aList = new ArrayList<String>();

How can I manipulate them if i want to access to a member of ArrayList. I tried converting both arrays to String but it doesn't give solution.

GlacialMan
  • 579
  • 2
  • 5
  • 20

1 Answers1

66
ArrayList<String> aList = new ArrayList<String>(Arrays.asList(question1));
aList.addAll(Arrays.asList(question2));
user3114639
  • 1,895
  • 16
  • 42
  • 1
    is it posible to add more arrays in list in the same line of the code, something like aList.addAll(Arrays.asList(question1,question2,question3...)); – GlacialMan Apr 11 '16 at 22:44
  • and how i can now to access to some of elements of array – GlacialMan Apr 11 '16 at 22:46
  • @GlacialMan *"is it posible to add more arrays in list in the same line of the code"* not really; *"and how i can now to access to some of elements of array"* - `aList.get(0);`, perhaps you should take a closer look at [Collections Trail](http://docs.oracle.com/javase/tutorial/collections/) – MadProgrammer Apr 11 '16 at 22:52
  • @GlacialMan or maybe take a look at [ArrayList API](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) – user3114639 Apr 11 '16 at 22:55
  • this will get me Array, but i want to access to element of an array, for example i want to work something with q4 element of Array question1. – GlacialMan Apr 11 '16 at 22:55
  • `aList.get(idx);` will give you the element at index idx of `aList`. As you know the order you've added your arrays to `aList`, you should know what is the index of your wanted element in `aList`. – user3114639 Apr 11 '16 at 22:58
  • aha ok so if i want to access to Q4 i will do aList.get(3); or if i want to access to Q7 i will do aList.get(6); – GlacialMan Apr 11 '16 at 23:04
  • If you refer to your example arrays- you're right :) – user3114639 Apr 11 '16 at 23:08
  • yeah ok that's it thank you :) – GlacialMan Apr 11 '16 at 23:10