You are on page 1of 5

Class Array.

java
package com.company.rohith;
import java.util.ArrayList;
import java.util.Scanner;
public class Array {
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
public void Append() {
System.out.println(" Enter the string to append");
String str = sc.next();
list.add(str);
System.out.println("Successfully appended String " + str);
}
public void Insert() {
System.out.println("Enter the index to insert");
int index = sc.nextInt();
System.out.println("Enter the string to insert");
String str = sc.next();
list.indexOf(index);
list.add(str);
System.out.println(str + " added at index " + index);
}
public void Search() {
System.out.println("Enter the string to search");
String str = sc.next();
if (list.contains(str)) {
System.out.println(" element Found");
System.out.println("Found at index " + list.indexOf(str));
} else {
System.out.println("Element not found");
}
}

public void ListString() {


System.out.println("Enter the letter to search");
String letter = sc.next();
System.out.println("Searching for the String...........");
for (String str : list) {
if (str.startsWith(letter)) {
System.out.println( str);
}
else{
System.out.println("Sorry element with letter " + letter + "not found....");
}
}
}
}

Class Arraylist.java
package com.company.rohith;
import java.util.Scanner;
public class Arraylist{
public static void main(String[] args) {
Array ap = new Array();
Scanner sc = new Scanner(System.in);
int ch;
do {
System.out.println("1. Append 2. Insert 3. Search 4. list of string with given
letters");
System.out.println("enter the choice");
int choice = sc.nextInt();
switch (choice) {
case 1: {
ap.Append();
break;
}
case 2: {
ap.Insert();
break;
}
case 3: {
ap.Search();
break;
}
case 4:{
ap.ListString();
break;
}

default:
System.out.println("invalid choice");
break;
}
System.out.println("enter 0 to quit 1 to continue ");
ch = sc.nextInt();

} while(ch==1);
}

output
1.Append 2.Insert 3.Search 4.list of string with given letters

enter the choice

Enter the string to append

Rahul

Successfully appended String Rahul

enter 0 to quit 1 to continue

1.Append 2.Insert 3.Search 4.list of string with given letters

enter the choice

Enter the index to insert

Enter the string to insert

Rohith

Rohith added at index 1

enter 0 to quit 1 to continue

1.Append 2. Insert 3. Search 4. list of string with given letters

enter the choice

Enter the string to search

Rahul

element Found

Found at index 0

enter 0 to quit 1 to continue

1.Append 2. Insert 3. Search 4. list of string with given letters

enter the choice


3

Enter the string to search

msdhoni

Element not found

enter 0 to quit 1 to continue

1.Append 2. Insert 3. Search 4. list of string with given letters

enter the choice

Enter the letter to search

Searching for the String...........

Rahul

Rohith

enter 0 to quit 1 to continue

You might also like