Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with arrayLists in java. I have a function that gets user input and does a few things which creates an arrayList known as values.

Help with arrayLists in java. I have a function that gets user input and does a few things which creates an arrayList known as values. Then I have another function that does selection sort on that ArrayList. However, the selection sort isn't working properly... here is the code:

import java.util.ArrayList; import java.util.Scanner; public class CIS231MultiInput { public static void main(String [] args) { // Store all values input

System.out.println(); ArrayList sortedArrayList = sortArrayList(); System.out.print(" " + sortedArrayList + " "); System.out.println(sortedArrayList.size() + " size "); } public static ArrayList createArrayList() { // <> on right side ("diamond") means create with same parameterized type as on left ArrayList values = new ArrayList<>(); Scanner keyIn = new Scanner(System.in); // input a line with any number of white-space-delimited values System.out.println("Enter a line of input with any number of integers"); System.out.println("All values must be white-space-delimited"); System.out.println("Non-integers will be discarded");System.out.print("->"); String nextLine = keyIn.nextLine(); // Apply scanning logic to this string to parse items and check for type Scanner lineScan = new Scanner(nextLine); // Repeat as long as we have unprocessed input while (lineScan.hasNext()) { // if next item is an int, add to arraylist, else discard if (lineScan.hasNextInt()) { values.add(lineScan.nextInt()); } else {// deliberately not assigned, merely "digested" lineScan.next(); } } System.out.println(" A total of " + values.size() + " integers were input: ");

// Output all values using "for in" loop from 2.4 for (Integer next : values) { System.out.print(next + " "); } return values; } public static ArrayList sortArrayList() { ArrayList someArrayList = createArrayList(); for(int i = 0; i < someArrayList.size() - 1; i++) { int minIndex = i;

for(int j = 1; i < someArrayList.size(); i++) { int temp = someArrayList.get(minIndex); someArrayList.set(minIndex, someArrayList.get(i)); someArrayList.set((i), temp); } } return someArrayList; } } Here is the pastebin for easier readability: https://pastebin.com/uf7hMsNd I'm trying to do selection sort. I'm not sure where I went wrong. I can show the selection sort code in python to compare.

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

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago