0% found this document useful (0 votes)
10 views

Class Array1

The document contains code for an Array class with methods to append, insert, search, and list strings from an ArrayList based on a letter search. It takes user input to select an operation and any needed parameters like the string or index. For each operation it prints the output, such as successfully adding a string or search results. The main method creates an Array object and runs a loop prompting the user selection until they choose to quit.

Uploaded by

rohtihr2611
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Class Array1

The document contains code for an Array class with methods to append, insert, search, and list strings from an ArrayList based on a letter search. It takes user input to select an operation and any needed parameters like the string or index. For each operation it prints the output, such as successfully adding a string or search results. The main method creates an Array object and runs a loop prompting the user selection until they choose to quit.

Uploaded by

rohtihr2611
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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