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 8 years ago.
Improve this question
I would like to have an array which holds a list of arrival times of trains like this
Arrival Times Array:
9.15
10.34
12.10
6.15
What would be the correct way to do it and i also want to sort them
Use class java.util.Date. The array declaration will look like
Date[] myDates = new Date[n];
where n is number of elements you need.
You can also create arraylist like below snippet
import java.util.*;
List<Calendar> dates = new ArrayList<Calendar>(n); // initial size if not given it will grow dynamically.
dates.add( Calendar.getInstance() );
for you case to simple hold the time of the trains you can use any arrays of the float or double like below
float floatArray[] = new float[5]; //if size is fixed or else use arraylist or
double doublearray[]=new double[5];
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 3 years ago.
Improve this question
If A is an array of type int, what command would you use to put the value 50 in the first position of the array
A[0] = 50;
This will store 50 in the first element of A.
A[0] = 50; This assign first element to 50.
There is a lot of resource about java array online, i think all the basic tutorial would cover about this.
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 3 years ago.
Improve this question
object Test extends App {
val i: Iterable[(String, Long)] = List(("a", 1), ("b", 2))
val sortedMap: SortedMap[String, Long] = i.toList.sortBy(_._2)
}
I don't want to convert Iterable to List/Array etc since it's coming form a jdbc query.
You can't do that. SortedMap sorts by keys, not values.
If you want it sorted by value, you gotta use ListMap, and can't avoid converting to List:
ListMap(i.toList.sortBy(-_._2):_*)
There isn't really too much wrong with converting to list, since you are loading the whole thing in memory anyway. This is faster too, than building a tree one element at a time.
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 7 years ago.
Improve this question
array = ArrayUtils.removeElement(array, array[i]);
This is working for int array, not for String array...
Is there any fast way to remove an element from a string array in Java?
Seems that you are using org.apache.commons.lang.ArrayUtils class, which is not standard Java class but part of Apache Commons Lang library. If you know an index of element, it would be more efficient to use
array = ArrayUtils.remove(array, i);
As this version will not search for given element, just remove by index. This should work for object arrays (including String array) as well.
Since array can not be resized, it may create some problems (eg.- raise of cost) when you try to remove some element from an array.
In this case ArrayList would be a better alternative. You can add/remove element from ArryList more continently -
List<String> strgs = new ArrayList<String>();
strgs.add("first");
strgs.add("second");
strgs.add("third");
strgs.remove("second");
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 8 years ago.
Improve this question
I know how to create arrays like so:
int[] myIntArray = new int[]{1,2,3};
My question is this. What if I had a file, titled Lab11Input.txt, and filled it with integer values. How would I go about creating a method that opens the file, counts the number of numbers in the file, creates the array, then fills the array with the values from the file?
For example, if I pass in Lab11Input.txt as an argument, could I do it that way?
A simple way using Scanner:
Scanner sc = new Scanner(new File ("Lab11Input.txt"));
List<Integer> ints = new ArrayList<>();
while(sc.hasNextInt()) {
ints.add(sc.nextInt());
}
// then you can convert ints to an array
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'm making a method that takes 2 inputs, one is int[][], the other is int. I need to store that int value as the "row" of the int array. For example if I input (int[][] array, int 3493), I need the row to be {3 4 9 3}. This is basically like the 2048 games, as I need to be able to manipulate the array later. I do not know the proper syntax for this, however. Please help.
Thank you.
As An Sample, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional
arrays and each contains four elements, as shown in the figure x.length is 3, and
x[0].length, x[1].length, and x[2].length are 4
As you see you need two for loops to go through the array.
1. for traversing the row one by one
2. when you are in each row traversing each column one by one
for example: