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.

  1. Create an ArrayList with elements the objects of previous class.
  2. Using a simple loop populate the ArrayList with data from some arrays with data, or from console.
  3. 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.
  4. Use the Scanner class to display the content of the file on console.
  5. 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 list = new 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 = new ArrayList(); //ArrayList of Strings

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 theUsers=new ArrayList(); for(int i=0;i<5;i++) { //creation of an object User aUser=new User(firstNames[i],lastNames[i], new Date()); theUsers.add(aUser); } // a loop that uses iterator Iterator iter = theUsers.iterator(); while (iter.hasNext()) System.out.println( iter.next() ); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!