Question
I need help with my java assignment. My code will be below, I need someone to edit it for me to fit these instructions. Create
I need help with my java assignment. My code will be below, I need someone to edit it for me to fit these instructions.
- Create an ArrayList with elements the objects of previous class.
- Using a simple loop populate the ArrayList with data from some arrays with data, or from console.
- Use a loop with iterator and write the information in a File using PrintWriter. Use this address for the file (c:\ esult\\MyData.txt). Be careful to create previously the folder result.
- Use the Scanner class to display the content of the file on console.
- Be careful to use trycatch to handle the exceptions for read and write operations related to the file.
package arraylist;
import java.util.ArrayList; import java.util.Iterator; import java.util.Date;
public class ArrayAssignment { public static void main(String[] args) { String[] firstNames= {"Tom", "David","Joel","Steven","Dora"}; String[] lastNames= {"Taylor", "Sony","Baky","Wood","August"}; ArrayList
//Adding elements to ArrayList list.add(10); list.add(20); list.add(30); list.add(40); System.out.println(list); //Output : [10, 20, 30, 40]
//Retrieving element at index 2 System.out.println(list.get(2)); //Output : 30
//Setting value of element at index 2 list.set(2, 2222); System.out.println(list); //Output : [10, 20, 2222, 40]
//Inserting element at index 1 list.add(1, 1111); System.out.println(list); //Output : [10, 1111, 20, 2222, 40]
//Removing element from index 3 list.remove(3); System.out.println(list); //Output : [10, 1111, 20, 40] ArrayList
sList.add("First"); sList.add("Second"); sList.add("Third"); sList.add("Fourth"); System.out.println(sList); //Output : [First, Second, Third, Fourth] //Retrieving position of "Second" element System.out.println(sList.indexOf("Second")); //Output : 1 // some loop examples for each loop for( String s : sList) //for each loop System.out.println( s+" is the "+sList.indexOf(s)+ "th element" ); int sum=0; // calculation of sum of the elements of the first list for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started