navdeep Posted August 12, 2012 Share Posted August 12, 2012 consider- String[] str1={"One"}; String[] str2={"One","Two"}; String[] str3={"One","Two", "Three"}; String[] str4={"One","Two", "Three", "Four"}; String[] str5={"One","Two", "Three", "Four","Five"}; ArrayList al=new ArrayList(); al.add(str1); al.add(str2); al.add(str3); al.add(str4); al.add(str5); till now i have successfully added 5 arrays in the array list, al.. now i want to retrieve each element of each array.. if i use iterator- Iterator itr=al.iterator(); while(itr.hasNext()) { System.out.println("Element : "+itr.next()); } i get- Element : [Ljava.lang.String;@182f0db Element : [Ljava.lang.String;@192d342 Element : [Ljava.lang.String;@6b97fd Element : [Ljava.lang.String;@1c78e57 Element : [Ljava.lang.String;@5224ee clearly, these are addresses of those 5 arrays, i dont want them, i want the elements, output should be- One One Two One Two Three One Two Three Four One Two Three Four Five so my question, how to retrieve them? but i guess i can achieve this by using List and probably even achieve my goal but what if i only want the value "Three" from the array str3 that is now in al, how would i retrieve it? desired output- Value at 3rd position of al's 3rd element : Three Link to comment Share on other sites More sharing options...
Greg H. Posted August 12, 2012 Share Posted August 12, 2012 Off the cuff I would say public static void main(String[] args) { String[] str1={"One"}; String[] str2={"One","Two"}; String[] str3={"One","Two", "Three"}; String[] str4={"One","Two", "Three", "Four"}; String[] str5={"One","Two", "Three", "Four","Five"}; ArrayList<String[]> al=new ArrayList<String[]>(); al.add(str1); al.add(str2); al.add(str3); al.add(str4); al.add(str5); String[] array = (String[]) al.get(2); System.out.print(array[2]); } Str 3, position 3 :Three If you wanted to iterate over all of them you could do something like this: public static void main(String[] args) { String[] str1={"One"}; String[] str2={"One","Two"}; String[] str3={"One","Two", "Three"}; String[] str4={"One","Two", "Three", "Four"}; String[] str5={"One","Two", "Three", "Four","Five"}; ArrayList<String[]> al=new ArrayList<String[]>(); al.add(str1); al.add(str2); al.add(str3); al.add(str4); al.add(str5); Iterator itr = al.iterator(); while(itr.hasNext()){ String[] array = (String[]) itr.next(); for (int i = 0;i < array.length; i++){ System.out.print(array[i] + " "); } System.out.print("\n"); } } } Output: One One Two One Two Three One Two Three Four One Two Three Four Five 1 Link to comment Share on other sites More sharing options...
navdeep Posted August 13, 2012 Author Share Posted August 13, 2012 thanks 'Greg H.' for the prompt reply.. my problem is solved now.. but i figured one more way of iterating through the array list, thought i would share with you, it goes as follows: import java.util.*; class al { public static void main(String nav[]) { String[] st1={"One"}; String[] st2={"One", "Two"}; String[] st3={"One", "Two", "Three"}; String[] st4={"One", "Two", "Three", "Four"}; String[] st5={"One", "Two", "Three", "Four", "Six"}; ArrayList obj=new ArrayList(); obj.addAll(Arrays.asList(st1, st2, st3, st4, st5)); for(int i=0; i<obj.size(); i++) System.out.println(Arrays.toString((String[]) obj.get(i))); } } Output- [One] [One, Two] [One, Two, Three] [One, Two, Three, Four] [One, Two, Three, Four, Six] thanking you again.. Link to comment Share on other sites More sharing options...
Greg H. Posted August 13, 2012 Share Posted August 13, 2012 Happy to help. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now