I have a string array and I am just initializing it as below in a java file (Constants.java)
public static final String[] MY_ARRAY = {"xyz","pqr","trw","abc"};
I am using this MY_ARRAY in another java file in main method as below:
String [] HEADER = Constants.MY_ARRAY;
// MY_ARRAY is coming as sorted in natural order ("abc","pqr","trw","xyz").
This is an intermittent issue and not happening every time I run my program. I also have another final static String arrays like MY_ARRAY and have different strings. I am facing same issue with all these string arrays. I cant say when exactly this will happen but it happens intermittently.
Thanks in advance.
Related
I have a static 2 dimensional array of objects in a Kotlin project:
class Tables {
companion object{
lateinit var finalTable: Array<Array<Any?>?>
}
}
It is a little clearer in Java:
public class Tables {
public static Object[][] finalTable;
}
The third element in one row of objects in the table, is a string boxed as an object. In other words: finalTable[*][2] is a string describing the item. When I add an item to the array in Kotlin, I want to sort the entire array in alphabetical order of the description.
In Java this is easy:
Arrays.sort(Tables.finalTable, Comparator.comparing(o -> (String) o[2]));
When I try to use Android Studio to translate the Java code into Kotlin, it produces the following:
Arrays.sort( Tables.finalTable, Comparator.comparing( Function { o: Array<Any?>? -> o[2] as String }) )
This does not work, you have change the String cast as follows:
Arrays.sort( Tables.finalTable, Comparator.comparing( Function { o: Array<Any?>? -> o[2].toString() }) )
This version will compile and run, but it totally messes up the sorting of the table, so it does not work. I have tried variations on this theme, without any success. To get my project to work, I had to create a Java class in the Kotlin project with the functional Java code listed above:
public class ArraySort {
public void sortArray(){
Arrays.sort(Tables.finalTable, Comparator.comparing(o -> (String) o[2]));
}
}
This sorts the table like a charm, but I would prefer to keep my project "pure Kotlin". Can anyone suggest a pure Kotlin method to sort such an array? Thanks!
Unless I'm missing something, you can just do this:
Tables.finalTable.sortBy { it[2] as String }
which sorts your array in place. sortedBy will produce a new copy of the original if that's what you want instead, and might be why the comment suggestions weren't working for you.
But this whole unstructured array situation isn't ideal, the solution is brittle because it would be easy to put the wrong type in that position for a row, or have a row without enough elements, etc. Creating a data structure (e.g. a data class) would allow you to have named parameters you can refer to (making the whole thing safer and more readable) and give you type checking too
The Problem:
I have a semi-large [Int] array, which contains ~ 25K to 60K elements. It was originally outputted into a text file by my first program and needs to be fed as a let value into a second program. To do this, I manually copied it in. However, Swift just freezes whenever I try to initialize it. By freeze, I mean that it doesn't do anything, even after an hour.
Further investigation:
I confirmed that it was the size of the array causing the freeze-up, by creating a test program with just one line that just said let test = [the_array]. That program is still running after an hour.
I have previously used arrays that contained 400-450K elements without any problems. However, those arrays didn't have to be initialized as a variable/constant, and each element only contained a number between 1-9. The array that is causing the freeze-up definitely has less than 100K elements, but each element contains an integer between 100-300K.
How am I able to initialize the array into the second program, and what exactly is causing the problem?
Other info: I'm using Swift 3.1.1 on Ubuntu 16.04 64bit, so I don't have access to Xcode. If you need the text file of the array, please leave a comment.
File is uploaded here
I downloaded your file - a 600KB file is a very small size for any modern computer. This codes ran in under 1 second on my 2012 iMac:
let fileURL = URL(fileURLWithPath: "/path/to/file.txt")
let charset = CharacterSet.whitespacesAndNewlines.union(CharacterSet(charactersIn: "[]"))
let fileContent = try! String(contentsOf: fileURL).trimmingCharacters(in: charset)
let array = fileContent.components(separatedBy: ",").flatMap {
Int($0.trimmingCharacters(in: .whitespaces))
}
print(array.count) // 74061
I'm not a programmer, but I find myself writing some simple ruby and aren't sure about a few things.
I have the following function
def resolve_name(ns_name)
ip = Resolv.getaddress(ns_name)
return ip
end
and the array
array = ['ns-1.me.com', 'ns-2.me.com']
What I want to do is to pass every element in the array to the function to be evaluated, and spit out to... something. Probably a variable. Once I have the resolved IPs I'll be passing them to an erb template. Not quite sure yet how to handle when there may be 1 to 4 possible results either.
What I want think I need to do is do an each.do and typecast to string into my function, but I haven't been able to figure out how to actually do that or phrase my problem properly for google to tell me.
http://ruby-doc.org/core-2.0.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversion Doesn't quite have what I'm looking for.
irb(main):010:0> resolved = resolve_name(array)
TypeError: no implicit conversion of Array into String
Any suggestions?
Take a look at the documentation for ruby's Enumerable, which arrays implement. What you're looking for is the map method, which takes each element of an enumerable (i.e. an array) and passes it to a block, returning a new array with the results of the blocks. Like this:
array.map{|element| resolve_name(element) }
As an aside, in your method, you do not need to use a local variable if all you're doing with it is returning its value; and the return statement is optional - ruby methods always return the result of the last executed statement. So your method could be shortened to this:
def resolve_name(ns_name)
Resolv.getaddress(ns_name)
end
and then you really all it's doing is wrapping a method call in another. So ultimately, you can just do this (with array renamed to ns_names to make it self-explanatory):
ns_names = ['ns-1.me.com', 'ns-2.me.com']
ip_addresses = ns_names.map{|name| Resolv.getaddress(name) }
Now ip_addresses is an array of IP addresses that you can use in your template.
If you pass an array you could do:
def resolve_name(ns_name)
res = []
ns_name.each do |n|
res << {name: n, ip: Resolv.getaddress(name) }
end
res
end
And get an array of hashes so you know which address has which ip
I want to create an array of JEditorPane depending on the size of a String array.
Is there a possibility to create an array of JEditorPane? If yes, how?
Here is an example:
String [] elements = {"0","1","2","3","4"};
JEditorPane ePane [] = new JEditorPane[5];
I want to to put each String element into the certain JEditPane, i.e
JEditorPane[0].setText(elements[0]);
etc. But I get a nullpointerexception when run I run.
Your problem is, that Java initializes a new array with the default value for the given type. In this case it is null, because the JEditorPane inherits from Object.
You cannot call a method on null - that is where the NullPointerException comes from.
The solution: make a loop in which you initialize the JEditorPane-objects in the array.
Then you can do JEditorPane[0].setText(elements[0]);
I have two Vectors one called "SET_grid" which should never me changed and one called "tmp_grid" which can, but how do i copy SET_grid to tmp_grid without binding it to the original, so if tmp_grid change then the SET_gird doesn't, these Vectors are both multidimensional e.g.
public var tmp_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
public var SET_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
so i would use them like this....
tmp_grid[x][y].sayhello();
tmp_grid = SET_grid does not work
tmp_grid = SET_grid.concat(); // nor does this one
Any help would be great
Nested arrays cannot be cloned without iterations. It's because they're nested :)
What this means is that you have to use nested loops and push to second vectors..