Question

can someone please help me with my computer science homework. i am not sure if i am doing it correctly i would like your answer so i can compare with mine

Create an ArrayList with the below state codes in it. AL AK AZ AR CA TX MA Once the ArrayList is created do below operations

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Java Code:-

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   ArrayList<String> states_code=new ArrayList<String>();
   states_code.add("AL");
   states_code.add("AK");
   states_code.add("AZ");
   states_code.add("AR");
   states_code.add("CA");
   states_code.add("TX");
   states_code.add("MA");
  
   //adding PA at the end of the ArrayList.
   states_code.add("PA");
  
   // Replace CA with FL in ArrayList.
   for(int i=0;i<states_code.size();i++)
   {
   if(states_code.get(i).equals("CA"))
   states_code.set(i,"FL");
   }
     
   //Loop through arrayand print the state code and its index postion.
   System.out.println("Index State");
   for(int i=0;i<states_code.size();i++)
   System.out.println(i+" "+states_code.get(i));
     
   // Remove the new state added PA (which is the last element of the ArrayList).
   states_code.remove(states_code.size()-1);
   }
}

8 9 12 Main.java 1 import java.util.*; 2 public class Main 3.{ 4 public static void main(String[] args) { 5 ArrayList<String>input State AL Index O 1 2 3 AK AZ AR 4 FL TX 5 6 7 MA PA ... Program finished with exit code 0 Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
can someone please help me with my computer science homework. i am not sure if i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT