/* related posts with thumb nails */

A program to fill elements into a list. Also, copy them in reverse order into another list.:

import java.io.*;

import java.util.*;

class ListTest

{

public static void main(String as[]) throws Exception

{

int i,j,n;

ArrayList l1=new ArrayList();

ArrayList l2=new ArrayList();

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

System.out.println("How many Strings:");

n=Integer.parseInt(br.readLine());

System.out.println("Enter strings:\n");

for(i=0;i

l1.add(br.readLine());

System.out.println("List Elements:\n"+l1);

for(i=n-1;i>=0;i--)

l2.add(l1.get(i));

System.out.println("List Elements Reversed:\n"+l2);

}

}

/* Output: */

How many Strings:4

Enter strings:

cat

rat

bat

List Elements:

[cat, rat, bat]

List Elements Reversed:

[bat, rat, cat]

Related Topics:

0 comments:

Post a Comment