Bifalgorithm post · Computer science one · Comparisons

How can you create arrays and collections of strings in Java?

How can you create arrays and collections of strings in Java? (Computer science one · Comparisons) — "We can create our own arrays of strings similar to how we…

Ideas in this flowchart

  • String collections can be created with arrays or dynamic collections.
  • Arrays of strings work similarly to arrays of int and double values.
  • An array of five strings is declared with String names[] = new String[5].
  • The five array positions are assigned names such as Margaret Hamilton, Ada Lovelace, Grace Hopper, Marie Curie, and Hedy Lamarr.
  • Dynamic collections such as List or Set can also store strings.
  • A string list is declared with List<String> names = new ArrayList<String>().
  • The names are inserted using repeated names.add(...) operations.
  • The list contains the same five names as the array example.
  • names.get(2) retrieves the element at index 2.
  • The retrieval prints "Grace Hopper".
Feed/

How can you create arrays and collections of strings in Java?

No cover

Channel

Computer science one

1 page

"We can create our own arrays of strings similar to how we created arrays of int and double types.\n\nString names[] = new String[5];\n\nnames[0] = \"Margaret Hamilton\";\n\nnames[1] = \"Ada Lovelace\";\n\nnames[2] = \"Grace Hopper\";\n\nnames[3] = \"Marie Curie\";\n\nnames[4] = \"Hedy Lamarr\";\n\nBetter yet, we can use dynamic collections such as a List or a Set of strings.\n\nList<String> names = new ArrayList<String>();\n\nnames.add(\"Margaret Hamilton\");\n\nnames.add(\"Ada Lovelace\");\n\nnames.add(\"Grace Hopper\");\n\nnames.add(\"Marie Curie\");\n\nnames.add(\"Hedy Lamarr\");\n\nSystem.out.println(names.get(2)); //prints \"Grace Hopper\""

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.