Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a method that randomly selects objects from an arraylist. Those objects are being added to another arraylist I created. How can I make it so that the same object is not added twice?
Let's say I have an arraylist:
["Chicken", "Dinner", "Noodles"]
I have another arraylist that I want to add values randomly from the first arraylist:
[]
So I use:
Math.random to get values from 0-2 and add them to the new arraylist.
Let's name the empty one list2 and the top one firstlist
So:
Assuming integer i is a random number 0-2.
list2.add(firstlist.get(i));
Yet this has a chance to add the same value, how can I check through list2, to make sure it hasn't been added yet, so I can not add it and pick another value?
Replace the other list with a java.util.Set. If your objects have correct hashCode() and equals() then they won't be added twice. In other words, use the right collection type for the job.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can someone please explain the use of the following code in Java
private TableColumn<Books, Integer> colId;
I want to know the reason of using <?,?> to declare a variable in Java.
Those are 'Generics'. Specifically, the things inside the brackets are called 'Type Parameters'. It allows different types to be chosen when you create the object or 'variable' as you call it.
So you could have a list of numbers with: new ArrayList<Integer>()
or a list of strings with: new ArrayList<String>()
https://www.tutorialspoint.com/java/java_generics.htm
In your example, it's creating a column to keep track of ids for books. The ids will be ints and the object they are identifying is books. But, you may want another TableColumn (which behaves the same as the Book Id column) but for tracking the titles of the books: TableColumn<Books, String> colTitle
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have used java ArrayList when I inserted one element in the list and i have converted the validatable response to array list when I used assert equals it is showing that both are different
like one is [abcd] other is [[abcd]]
Validatable response = given().spec(request).filter(new ErrorLoggingFilter(errorPrintStream)).pathParams("","").when.post(endpoint).then()
the response is of the form ArrayList when I printed that It came of the form [[abcd]]
To my knowledge, these two are different things
["abcd"] this means an array has one string "abcd" element.
[["abcd"]] this means an array has one array ["abcd"] element.
Yes, ["abcd"] and [["abcd"]] are completely different. Let us understand why.
Let us consider an array ["abcd"]. As you can see, it contains only one element i.e. "abcd". So this is an array that contains a single string value. Now for [["abcd"]], the outer array contains another array inside of it and the inner array contains "abcd". Though their ultimate content seem to be same, they are absolutely different. One is a string array (an array that contains a string value) and the other is an array of string arrays.
Absolutely different, a one-dimensional array, a two-dimensional array,in many languages,reference form it, [0] and [0][0]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am working on a project that has a list of student name and numbers, for example
James Bloggs,1
Paul Jonson,43
Andt Peters,23
Once I have them in an array I then need them sorted.
What is the best way of going about this. Its not the sort Im stuck on its the referencing the names to the numbers. I would have thought if I do a 2 denominational array only one would be sorted.
Any help would be great,
EDIT: I just realized this question was asking about a 2-dimensional array and my answer doesn't directly deal with that. I am skeptical that arrays should be involved at all. Arrays are usually for dealing with primitive data, and maybe if you are coming from a C background you'd think they'd be the natural thing to use. If you really honestly have to use arrays then this probably isn't the way to go.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
public void foo(){
// Use a TreeMap. It will sort keys on insertion.
Map<Integer,String> nameByNumber = new TreeMap<>();
nameByNumber.put(1, "James Boggs");
// etc. put all the entries in however you need to
List<Integer> sortedNumbers = personByNumber.getKeys();
List<String> namesSortedByNumber = personByNumber.getNames();
}
If you need it to be more organized and complex, you can encapsulate the name and number into a Class with a name and number property. Then you'd still use the number as the key, but you'd have the full class as the value. Do this if you need to have more than just a name, like last name, first name, address, etc.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm preparing for an exam. I can read the CSV file but I don't know how to sort a data line.
For example, if "SchoolName.csv" has
Cayuga,Elkhart,Slocum,Westwood
Neches,Palestine,central
Dibolon
Lufkin,Holiday
After I sort the data the output should be
Dibolon // becuase it only contain one name.
Lufkin,Holiday
Neches,Palestine,central
Cayuga,Elkhart,Slocum,Westwood
So you want to sort based on number of elements. Assuming you have those elements in a Collection<String> csvlines:
csvlines.stream()
.sorted(Comparator.comparing(line -> StringUtils.countMatches(line, ",")))
.collect(Collectors.toList())
If you want to sort each row based on the number of names, you could do it like this
// Read the file's lines
List<String> lines = Files.readAllLines(pathToFile);
List<String> sortedLines = lines.stream().sorted(Comparator.comparing(line -> line.split(",").length)).collect(Collectors.toList());
Define a comparator, That is a class extending comparator and pass it as an argument to arraylistname.sort(comparator);
A comparator returns a negative integer, zero, or a positive integer if the first argument is less than, equal to, or greater than the second.
so
class CompareByLength implements Comparator {
compare(list a, list b) {
return a.size()-b.size();
}
}
alternatively use b.size()-a.size() if you want your list ordered the other way round.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array list. In that list I have a couple of string elements and few integer elements. I need to change all the elements in my arraylist to string so that I can modify the string elements. So, when I am doing this, even the integer elements in my arraylist are being changed to string. Would that be a problem if tried to access the integer elements after they are changed to string?
It is possible to use parseInt() so no it is not a problem. You can read more here: http://www.tutorialspoint.com/java/number_parseint.htm
You shouldn't attempt to alter the types of elements contained in a list. Instead, create a new list of strings, and add to it appropriately:
List<String> strings = new ArrayList<>(integers.size());
for (Integer a : integers)
strings.add(a.toString());