Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions

Question

What was the cause of action?

Answered: 1 week ago