• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 23:57
CET 04:57
KST 12:57
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL20] Finals Preview: Arrival13TL.net Map Contest #21: Voting10[ASL20] Ro4 Preview: Descent11Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9
Community News
2025 RSL Offline Finals Dates + Ticket Sales!9BSL21 Open Qualifiers Week & CONFIRM PARTICIPATION1Crank Gathers Season 2: SC II Pro Teams9Merivale 8 Open - LAN - Stellar Fest3Chinese SC2 server to reopen; live all-star event in Hangzhou23
StarCraft 2
General
Could we add "Avoid Matchup" Feature for rankgame RotterdaM "Serral is the GOAT, and it's not close" The New Patch Killed Mech! Chinese SC2 server to reopen; live all-star event in Hangzhou Weekly Cups (Oct 13-19): Clem Goes for Four
Tourneys
Crank Gathers Season 2: SC II Pro Teams 2025 RSL Offline Finals Dates + Ticket Sales! Merivale 8 Open - LAN - Stellar Fest $5,000+ WardiTV 2025 Championship $3,500 WardiTV Korean Royale S4
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 497 Battle Haredened Mutation # 496 Endless Infection Mutation # 495 Rest In Peace Mutation # 494 Unstable Environment
Brood War
General
BW General Discussion [ASL20] Ask the mapmakers — Drop your questions BSL Team A vs Koreans - Sat-Sun 16:00 CET [ASL20] Finals Preview: Arrival BSL Season 21
Tourneys
[ASL20] Grand Finals The Casual Games of the Week Thread BSL21 Open Qualifiers Week & CONFIRM PARTICIPATION ASL final tickets help
Strategy
How to stay on top of macro? PvZ map balance Soma's 9 hatch build from ASL Game 2 Current Meta
Other Games
General Games
General RTS Discussion Thread Stormgate/Frost Giant Megathread Path of Exile Nintendo Switch Thread Dawn of War IV
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine US Politics Mega-thread YouTube Thread The Chess Thread
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
Movie Discussion! Anime Discussion Thread [Manga] One Piece Korean Music Discussion Series you have seen recently...
Sports
MLB/Baseball 2023 2024 - 2026 Football Thread Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
Reality "theory" prov…
perfectspheres
LMAO (controversial!!)
Peanutsc
The Benefits Of Limited Comm…
TrAiDoS
Our Last Hope in th…
KrillinFromwales
Certified Crazy
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1099 users

The Big Programming Thread - Page 600

Forum Index > General Forum
Post a Reply
Prev 1 598 599 600 601 602 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
Last Edited: 2015-03-04 19:48:43
March 04 2015 19:26 GMT
#11981
== is fine for std::string comparisons in C++

it's a bit more readable than std::equal(stringA.first(), stringA.last(), stringB.first())


char * and char[] strings not so much...

There is no one like you in the universe.
Millitron
Profile Blog Joined August 2010
United States2611 Posts
March 04 2015 19:27 GMT
#11982
On March 05 2015 04:15 RoyGBiv_13 wrote:
Show nested quote +
On March 05 2015 03:54 travis wrote:
what is wrong with comparing strings with == or != ?


A String variable is effectively a pointer to memory where the characters that make up the string are located. By comparing Strings using == and !=, you're comparing the pointers.

In C, C++, and C#, this won't work 80% of the time, as the same string may be located at different memory locations. It will work on the occasion that you do:

typedef string char[];
string a = "foo";
string b = a;


In Java, Strings are immutable types. The Java interpreter knows of a special property with immutable types that it can use the same memory for identical Objects, so using the == and != in Java works since both String Objects point to the same place in memory.... sometimes. It's undefined how often or when it will work, since there are optimizations that depend on the length of the string, total number of Objects, memory used, generation in the garbage collection, etc.

In Python, the immutable types optimization comes up again, with the added difficulty of type coercion. Using "is" instead of "==" will result in the Strings actually being compared, where using "==" will check that they both point to the same String. If one does not point to a String, you may get NaN instead of True or False... !Fun!

In Javascript, "==" will coerce types causing certain corner cases to give odd results, such as NaN instead of False."===" will probably do the string equality you want instead.

In Java, Strings have a isEqual() method which you should use for comparing strings. There's also a compareTo() method for alphabetical order.
Who called in the fleet?
Logo
Profile Blog Joined April 2010
United States7542 Posts
March 04 2015 19:47 GMT
#11983
How do people feel about file naming across larger projects? Should file names be unique on search, or is it ok to rely on the folder path to distinguish different files and what trade-offs do people try to make?

I'm trying to write up some standards for file/folder structure, but am struggling a bit on how people should name file names.

As a rough example take a structure like: (In these examples bar/baz/other would be represent system objects or components rather than generic folders like "utils")

foo/bar/
  • key-bindings.x
  • my-class.x
  • another-class.x


foo/baz/
  • key-bindings.x
  • my-class.x


foo/ball/
  • key-bindings.x


utils
  • date-picker.x


vs. a scheme like:

foo/bar/
  • bar-key-bindings.x
  • bar-my-class.x
  • bar-another-class.x


foo/baz/
  • baz-key-bindings.x
  • baz-my-class.x


foo/ball/
  • ball-key-bindings.x


utils
  • date-picker.x


The tradeoff here is in the first scheme when viewed through an explorer tree or searched for under a constrained search context things are easy to distinguish and the filenames are shorter without losing any information when the context is viewed.

But in the latter case when people open files through an large net search or have multiple files open in multiple tabs the files are easier to distinguish and there's less confusion. Searching for "baz" in an open file dialog will quickly return results that are relevant rather than searching "key-bindings" and then having to sort through the potentially dozens of matches to see which one has the relevant folder path. This problem is especially pronounced if you have something like a module based folder path/packing where you may have ui/keybindings as the last part of the structure for every key-bindings.x file and users have to look at the middle part of the path to distinguish the files.
Logo
MichaelEU
Profile Joined February 2011
Netherlands816 Posts
Last Edited: 2015-03-04 20:06:18
March 04 2015 20:05 GMT
#11984
On March 05 2015 04:15 RoyGBiv_13 wrote:
In Python, the immutable types optimization comes up again, with the added difficulty of type coercion. Using "is" instead of "==" will result in the Strings actually being compared, where using "==" will check that they both point to the same String. If one does not point to a String, you may get NaN instead of True or False... !Fun!


Eh, no? "==" compares values, "is" compares identity. But you're right that some optimization occurs with immutable types.

+ Show Spoiler +

a = 1999
b = 1998 +1
>>> a is b
False
>>> a == b
True


Optimization:

a = 3
b = 2+1
>>> a is b
True
>>> a == b
True




To the above poster, stick to relying on folder path.
世界を革命する力を!― znf: "Michael-oniichan ( *^▽^*)ノ✩キラ✩"
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
March 04 2015 20:22 GMT
#11985
On March 05 2015 04:15 RoyGBiv_13 wrote:
In C, C++, and C#, this won't work 80% of the time, as the same string may be located at different memory locations. It will work on the occasion that you do:

It's perfectly fine in C# if you use the built in string type. It has its operators overloaded to behave like a value type except for the nullable part. So == is the actual string comparision. If you want to compare C# strings by reference, you'll have to use ReferenceEquals(string, string).
If you have a good reason to disagree with the above, please tell me. Thank you.
Ben...
Profile Joined January 2011
Canada3485 Posts
March 05 2015 02:08 GMT
#11986
On March 05 2015 04:26 Blisse wrote:
== is fine for std::string comparisons in C++

it's a bit more readable than std::equal(stringA.first(), stringA.last(), stringB.first())


char * and char[] strings not so much...


Yeah for character arrays as strings you're better of with strcmp or similar functions, but yeah for std::strings in C++, "==" is fine if you are concerned if the contents of two strings are the same. Likewise for C#. Java is the outlier here, where "==" fails and .equals() is required to compare them.

Of course references are a different beast but that's not what was being asked originally.
"Cliiiiiiiiiiiiiiiiide" -Tastosis
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
Last Edited: 2015-03-05 04:05:42
March 05 2015 03:53 GMT
#11987
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-03-05 04:10:16
March 05 2015 04:03 GMT
#11988
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?


ArrayList1.add( ArrayList2.getIndex( Object X ), ArrayList2.remove( ArrayList2.getIndex( Object X ) ) );
ArrayList2.add( ( ArrayList1.getIndex( Object Y ) - 1 ), ArrayList1.remove( ArrayList1.getIndex( Object Y ) ) );


Pretty sure that will work. The remove method returns the object which is the second parameter of the add method. The first parameter is the index where the object will be inserted. The second call requires a minus one on the index because the Object X is in the old position.
I'll always be your shadow and veil your eyes from states of ain soph aur.
Deleted User 3420
Profile Blog Joined May 2003
24492 Posts
March 05 2015 04:09 GMT
#11989
Hard to wrap my brain around that LOL, but I will try it
Blitzkrieg0
Profile Blog Joined August 2010
United States13132 Posts
Last Edited: 2015-03-05 04:23:38
March 05 2015 04:10 GMT
#11990
On March 05 2015 13:09 travis wrote:
Hard to wrap my brain around that LOL, but I will try it


I just mashed the add and remove calls into one statement so that there is no temp storage involved. If you know which index you're moving it gets a lot simpler as you can just use the number instead of the getIndex method each time. Keep in mind you'll need to add 1 to the index on the second call because the Object X now resides at the original index and the object that is to be moved got pushed back one index.

Object temp;
temp = ArrayList1.remove( index );
ArrayList2.add( index, temp );
temp = ArrayList2.remove( index + 1 );
ArrayList1.add( index, temp );


Code with known index with no temp
ArrayList1.add( index, ArrayList2.remove( index ) );
ArrayList2.add( index, ArrayList1.remove( index + 1 ) );


Being able to read your code is valuable though so be careful which corners you cut.
I'll always be your shadow and veil your eyes from states of ain soph aur.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
March 05 2015 04:41 GMT
#11991
On March 05 2015 13:03 Blitzkrieg0 wrote:
Show nested quote +
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?


ArrayList1.add( ArrayList2.getIndex( Object X ), ArrayList2.remove( ArrayList2.getIndex( Object X ) ) );
ArrayList2.add( ( ArrayList1.getIndex( Object Y ) - 1 ), ArrayList1.remove( ArrayList1.getIndex( Object Y ) ) );


Pretty sure that will work. The remove method returns the object which is the second parameter of the add method. The first parameter is the index where the object will be inserted. The second call requires a minus one on the index because the Object X is in the old position.


In the spirit of code you'll hate yourself for 2 days from now, can't you do -- assuming you already had obj1, obj2 you wanted to swap.

Basically goal here is at the end -
list2[obj2idx] = obj1
list1[obj1idx] = obj2

 
list2.set(list2.indexOf(obj2), list1.set(list1.indexOf(obj1), obj2))
RIP GOMTV. RIP PROLEAGUE.
ZenithM
Profile Joined February 2011
France15952 Posts
Last Edited: 2015-03-05 04:49:58
March 05 2015 04:44 GMT
#11992
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up

The solutions above are probably all correct, but you might want to wonder if you picked the right collection for the job. If you need swapping instances between 2 collections, and especially if you want to do it efficiently, you might want to choose something else, like, for example, regular arrays. "Remove" on ArrayLists is an inefficient operation (that is to say, it has about the worst complexity that you can have for such an elementary operation).
One other option is to change the object themselves (change them into each other) if they're not too complex and if what you're interested in is the value of their fields and not their physical location in memory.

Edit: I'd probably go as far as to say that if you need a whole bunch of swapping for an extended period of time, with no "add" in the meantime, depending on the size of your arraylists, it may be worth it to just copy the arraylists in arrays (with toArray methods).
In short, it all depends on what you're trying to do, if you just want to exchange cards between player hands, you can stay with arraylists, if you're trying to implement some swap-based sorting algorithm, copy into swap-friendly collections beforehand.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
Last Edited: 2015-03-05 05:13:05
March 05 2015 05:09 GMT
#11993
On March 05 2015 13:44 ZenithM wrote:
Show nested quote +
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up

The solutions above are probably all correct, but you might want to wonder if you picked the right collection for the job. If you need swapping instances between 2 collections, and especially if you want to do it efficiently, you might want to choose something else, like, for example, regular arrays. "Remove" on ArrayLists is an inefficient operation (that is to say, it has about the worst complexity that you can have for such an elementary operation).
One other option is to change the object themselves (change them into each other) if they're not too complex and if what you're interested in is the value of their fields and not their physical location in memory.

Edit: I'd probably go as far as to say that if you need a whole bunch of swapping for an extended period of time, with no "add" in the meantime, depending on the size of your arraylists, it may be worth it to just copy the arraylists in arrays (with toArray methods).
In short, it all depends on what you're trying to do, if you just want to exchange cards between player hands, you can stay with arraylists, if you're trying to implement some swap-based sorting algorithm, copy into swap-friendly collections beforehand.


Huh? ArrayList is backed by a...array. You don't need to do the solution with any calls to remove using an ArrayList, don't need to drop down to a raw array for that. Calling set means no calls to remove/no array resizing, you can read ArrayList code.


/**
* Replaces the element at the specified position in this list with
* the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
rangeCheck(index);

E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}


Anyway, you can't really use a set/bag because those explictly are unordered collections and want order. The only problem is indexOf obviously isn't instant. I don't know best collection for you without knowing more of your code.

edit:
I mean I guess you have the range check call but ya that's pretty cheap. Obviously if this is the critical path of your hpc code, you'd be using fortran anyway, but outside that I don't think that it's worth dropping into a raw array for.

sorry if I'm missing some other good reason to use an array when you're otherwise using an arraylist.
RIP GOMTV. RIP PROLEAGUE.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
March 05 2015 05:22 GMT
#11994
Hi can someone help me out. Trying to generate an exe of a javafx app using maven. I see some stuff on the oracle site about the build task to add but I think that's for an ant build.

I can build the exe using the javafx packager cli tool, just would like to have it in the build process.

Here's my current POM - builds an executable Jar.

Also had a question about resources. I use some fxml files for layout (lol?) and so I have package com.example.passkeep, and the matching directory structure. The thing is now I ended up having the match that directory structure for my resources, (main/{src,resources}/com/example/passkeep), when for my resources I'd prefer to just have /main/resources/*.fxml.

+ Show Spoiler +


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 [url=http://maven.apache.org/maven-v4_0_0.xsd">]http://maven.apache.org/maven-v4_0_0.xsd">[/url]
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.passkeep</groupId>
<artifactId>pass-keep</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>pass-keep</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.passkeep.PassKeep</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

RIP GOMTV. RIP PROLEAGUE.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
March 05 2015 05:32 GMT
#11995
On March 05 2015 04:15 RoyGBiv_13 wrote:
Show nested quote +
On March 05 2015 03:54 travis wrote:
what is wrong with comparing strings with == or != ?


A String variable is effectively a pointer to memory where the characters that make up the string are located. By comparing Strings using == and !=, you're comparing the pointers.

In C, C++, and C#, this won't work 80% of the time, as the same string may be located at different memory locations. It will work on the occasion that you do:

typedef string char[];
string a = "foo";
string b = a;


In Java, Strings are immutable types. The Java interpreter knows of a special property with immutable types that it can use the same memory for identical Objects, so using the == and != in Java works since both String Objects point to the same place in memory.... sometimes. It's undefined how often or when it will work, since there are optimizations that depend on the length of the string, total number of Objects, memory used, generation in the garbage collection, etc.

In Python, the immutable types optimization comes up again, with the added difficulty of type coercion. Using "is" instead of "==" will result in the Strings actually being compared, where using "==" will check that they both point to the same String. If one does not point to a String, you may get NaN instead of True or False... !Fun!

In Javascript, "==" will coerce types causing certain corner cases to give odd results, such as NaN instead of False."===" will probably do the string equality you want instead.


In Java is it really undefined? Obviously I trust you as knowing a lot more about programming stuff then me based on your post histry, but Java doesn't have much undefined behavior I can think of unlike C/C++

I thought string literals you define in code are part constant pool so == work (along with the integer pool it maintains and stuff like that. Used to be perm gen but in Java 8 with perm gen gone idk how the whole constant pool works).

Any string you get otherwise won't compare with == unless you explicitly intern them?

POC.
code
+ Show Spoiler +


public class Main {

public static void main (String[] beans) {
String s1 = "test";
String s2 = "test";
Scanner s = new Scanner(System.in);
String s3 = s.next();

System.out.printf("%s: %d\n", s1, s1.hashCode());
System.out.printf("%s: %d\n", s2, s2.hashCode());
System.out.printf("%s: %d\n", s3, s3.hashCode());

System.out.println("s1 == s2");
System.out.println(s1 == s2);

System.out.println("s1.equals(s2)");
System.out.println(s1.equals(s2));

System.out.println("s1 == s3");
System.out.println(s1 == s3);

System.out.println("s1.equals(s3");
System.out.println(s1.equals(s3));

String interned = s3.intern();
System.out.println("s1 == s3");
System.out.println(s1 == interned);
}

}



run
+ Show Spoiler +


test
test: 3556498
test: 3556498
test: 3556498
s1 == s2
true
s1.equals(s2)
true
s1 == s3
false
s1.equals(s3
true
s1 == s3
true



RIP GOMTV. RIP PROLEAGUE.
ZenithM
Profile Joined February 2011
France15952 Posts
Last Edited: 2015-03-05 06:10:12
March 05 2015 06:03 GMT
#11996
On March 05 2015 14:09 teamamerica wrote:
Show nested quote +
On March 05 2015 13:44 ZenithM wrote:
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up

The solutions above are probably all correct, but you might want to wonder if you picked the right collection for the job. If you need swapping instances between 2 collections, and especially if you want to do it efficiently, you might want to choose something else, like, for example, regular arrays. "Remove" on ArrayLists is an inefficient operation (that is to say, it has about the worst complexity that you can have for such an elementary operation).
One other option is to change the object themselves (change them into each other) if they're not too complex and if what you're interested in is the value of their fields and not their physical location in memory.

Edit: I'd probably go as far as to say that if you need a whole bunch of swapping for an extended period of time, with no "add" in the meantime, depending on the size of your arraylists, it may be worth it to just copy the arraylists in arrays (with toArray methods).
In short, it all depends on what you're trying to do, if you just want to exchange cards between player hands, you can stay with arraylists, if you're trying to implement some swap-based sorting algorithm, copy into swap-friendly collections beforehand.


Huh? ArrayList is backed by a...array. You don't need to do the solution with any calls to remove using an ArrayList, don't need to drop down to a raw array for that. Calling set means no calls to remove/no array resizing, you can read ArrayList code.


/**
* Replaces the element at the specified position in this list with
* the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
rangeCheck(index);

E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}


Anyway, you can't really use a set/bag because those explictly are unordered collections and want order. The only problem is indexOf obviously isn't instant. I don't know best collection for you without knowing more of your code.

edit:
I mean I guess you have the range check call but ya that's pretty cheap. Obviously if this is the critical path of your hpc code, you'd be using fortran anyway, but outside that I don't think that it's worth dropping into a raw array for.

sorry if I'm missing some other good reason to use an array when you're otherwise using an arraylist.

Yes, I know arraylist is backed by an array, I guess I was arguing against the "insert + remove" method specifically, and that I had forgotten "set" for some reason :D. Still my point stands for "insert + remove", it's just not very good. And for other implementations of the List interface, "set" would be implemented as "insert + remove", so it's never a bad thing to be careful of.
Edit: Hmm yeah, I just hadn't seen the code in your post.
On March 05 2015 13:41 teamamerica wrote:
Show nested quote +
On March 05 2015 13:03 Blitzkrieg0 wrote:
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?


ArrayList1.add( ArrayList2.getIndex( Object X ), ArrayList2.remove( ArrayList2.getIndex( Object X ) ) );
ArrayList2.add( ( ArrayList1.getIndex( Object Y ) - 1 ), ArrayList1.remove( ArrayList1.getIndex( Object Y ) ) );


Pretty sure that will work. The remove method returns the object which is the second parameter of the add method. The first parameter is the index where the object will be inserted. The second call requires a minus one on the index because the Object X is in the old position.


In the spirit of code you'll hate yourself for 2 days from now, can't you do -- assuming you already had obj1, obj2 you wanted to swap.

Basically goal here is at the end -
list2[obj2idx] = obj1
list1[obj1idx] = obj2

 
list2.set(list2.indexOf(obj2), list1.set(list1.indexOf(obj1), obj2))

Edit2: Still, indexOf suffers from the same problem as remove (I know that you already know this teamamerica, I'm just saying that for travis :D). It's either you already know what indices you want to swap objects at (which I do guess is the case for travis), or you use another collection.
teamamerica
Profile Blog Joined July 2010
United States958 Posts
March 05 2015 06:31 GMT
#11997
On March 05 2015 15:03 ZenithM wrote:
Show nested quote +
On March 05 2015 14:09 teamamerica wrote:
On March 05 2015 13:44 ZenithM wrote:
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?



edit: just reread this post and wow, im pretty lazy
but I will leave the question up

The solutions above are probably all correct, but you might want to wonder if you picked the right collection for the job. If you need swapping instances between 2 collections, and especially if you want to do it efficiently, you might want to choose something else, like, for example, regular arrays. "Remove" on ArrayLists is an inefficient operation (that is to say, it has about the worst complexity that you can have for such an elementary operation).
One other option is to change the object themselves (change them into each other) if they're not too complex and if what you're interested in is the value of their fields and not their physical location in memory.

Edit: I'd probably go as far as to say that if you need a whole bunch of swapping for an extended period of time, with no "add" in the meantime, depending on the size of your arraylists, it may be worth it to just copy the arraylists in arrays (with toArray methods).
In short, it all depends on what you're trying to do, if you just want to exchange cards between player hands, you can stay with arraylists, if you're trying to implement some swap-based sorting algorithm, copy into swap-friendly collections beforehand.


Huh? ArrayList is backed by a...array. You don't need to do the solution with any calls to remove using an ArrayList, don't need to drop down to a raw array for that. Calling set means no calls to remove/no array resizing, you can read ArrayList code.


/**
* Replaces the element at the specified position in this list with
* the specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
rangeCheck(index);

E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}


Anyway, you can't really use a set/bag because those explictly are unordered collections and want order. The only problem is indexOf obviously isn't instant. I don't know best collection for you without knowing more of your code.

edit:
I mean I guess you have the range check call but ya that's pretty cheap. Obviously if this is the critical path of your hpc code, you'd be using fortran anyway, but outside that I don't think that it's worth dropping into a raw array for.

sorry if I'm missing some other good reason to use an array when you're otherwise using an arraylist.

Yes, I know arraylist is backed by an array, I guess I was arguing against the "insert + remove" method specifically, and that I had forgotten "set" for some reason :D. Still my point stands for "insert + remove", it's just not very good. And for other implementations of the List interface, "set" would be implemented as "insert + remove", so it's never a bad thing to be careful of.
Edit: Hmm yeah, I just hadn't seen the code in your post.
Show nested quote +
On March 05 2015 13:41 teamamerica wrote:
On March 05 2015 13:03 Blitzkrieg0 wrote:
On March 05 2015 12:53 travis wrote:
If I have 2 arraylists, both holding the same types of objects, and I want to swap an element of the first arraylist with an element of the 2nd arraylist (meaning they change places, and each move to the other arraylist and position), is there an easy way to do this?

Or am I going to have to do something way more complicated like get their indexes, move them into another arraylist for temporary storage, and then completing the swap by moving them out of the extra arraylist I am using?


ArrayList1.add( ArrayList2.getIndex( Object X ), ArrayList2.remove( ArrayList2.getIndex( Object X ) ) );
ArrayList2.add( ( ArrayList1.getIndex( Object Y ) - 1 ), ArrayList1.remove( ArrayList1.getIndex( Object Y ) ) );


Pretty sure that will work. The remove method returns the object which is the second parameter of the add method. The first parameter is the index where the object will be inserted. The second call requires a minus one on the index because the Object X is in the old position.


In the spirit of code you'll hate yourself for 2 days from now, can't you do -- assuming you already had obj1, obj2 you wanted to swap.

Basically goal here is at the end -
list2[obj2idx] = obj1
list1[obj1idx] = obj2

 
list2.set(list2.indexOf(obj2), list1.set(list1.indexOf(obj1), obj2))

Edit2: Still, indexOf suffers from the same problem as remove (I know that you already know this teamamerica, I'm just saying that for travis :D). It's either you already know what indices you want to swap objects at (which I do guess is the case for travis), or you use another collection.



Sorry if I came off as too combative.

Misunderstanding a few points:
>And for other implementations of the List interface, "set" would be implemented as "insert + remove", so it's never a bad thing to be careful of.

Set isn't implementation of List interface. But we're might be talking cross points here.

>indexOf suffers from the same problem as remove...

indexOf suffers from same probem as remove? Are you talking about it being O(n)? My bad - thought you were bringing up insert/removing triggering array resizing.

But I don't know how you'd work around the need to do this

indexOf isn't O(1) but neither would it be for an array b/c you still need to find the damn index in the array, unless you already know indices, in which case you don't need to use indexOf for the ArrayList method anyway (l1.set(o2idx,list.set(o1idx, l2.get(o2idx))), now that's all constant time operations.

>It's either you already know what indices you want to swap objects at (which I do guess is the case for travis), or you use another collection.

The code I posted assumes you don't know the indices. indexOf kind of sucks, but I don't know what better way there is to do it really, because I'm assuming the ordering of the list matters somehow, so I can't imagine easily dropping in other collection? Maybe having another bookeeping map where you track index of each object, combined with the list, but that has it's own tradoffs.

What other collection would you use? I get this feeling I'm missing some obvious way to have an arbitrarily ordere collection where you also know in O(1) or even O(log(n)) the index of an item in the collection (collections not sorted so you can't binary search).


Again, sorry if I'm misunderstanding something.
RIP GOMTV. RIP PROLEAGUE.
ZenithM
Profile Joined February 2011
France15952 Posts
Last Edited: 2015-03-05 08:23:34
March 05 2015 08:20 GMT
#11998
You can definitely implement some indexed collection over a hashtable with Integer keys, with another hashtable for reverse indexing. Not saying you should (;D), but you can, and indexOf would be O(1) in this case, as well as set, so your method would be in constant time. (There are also restrictions like being careful because you can't put the same object twice or shit like that)

>And for other implementations of the List interface, "set" would be implemented as "insert + remove", so it's never a bad thing to be careful of.

Set isn't implementation of List interface. But we're might be talking cross points here.

I was talking about the method set(int, T) of List<T>, not sets, sorry for that. For example you can use set on LinkedList as you would on ArrayList, but then it's surely in O(n) and not O(1).

Again, sorry if I'm misunderstanding something.

No worries mate, my point aren't very strong anyway, you were right in the first place
berated-
Profile Blog Joined February 2007
United States1134 Posts
March 05 2015 12:02 GMT
#11999
On March 05 2015 14:22 teamamerica wrote:
Hi can someone help me out. Trying to generate an exe of a javafx app using maven. I see some stuff on the oracle site about the build task to add but I think that's for an ant build.

I can build the exe using the javafx packager cli tool, just would like to have it in the build process.

Here's my current POM - builds an executable Jar.

Also had a question about resources. I use some fxml files for layout (lol?) and so I have package com.example.passkeep, and the matching directory structure. The thing is now I ended up having the match that directory structure for my resources, (main/{src,resources}/com/example/passkeep), when for my resources I'd prefer to just have /main/resources/*.fxml.

+ Show Spoiler +


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 [url=http://maven.apache.org/maven-v4_0_0.xsd">]http://maven.apache.org/maven-v4_0_0.xsd">[/url]
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.passkeep</groupId>
<artifactId>pass-keep</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>pass-keep</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.passkeep.PassKeep</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>



I have never tried it so no promises if it works but it looks like this might work:

http://zenjava.com/javafx/maven/native-bundle.html

As far as the classpath location goes, how are you actually referencing your fxml files? I just took a 15 second tutorial on fx and it shows something like this...

Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));


If that is what you are doing too, then, you just need to change how you are getting the resource. Maven requires that you split your classpath files into two parts, src/main/java and src/main/resources .. where as you know resources is for non java files. All of the stuff ends up on the classpath in the end though, so when you are getting the resource you just need to load it from the correct spot if you want to move it.

This might help explain it a bit more:

Link to stackoverflow post
heartlxp
Profile Joined September 2010
United States1258 Posts
March 05 2015 18:20 GMT
#12000
hi guys, anyone else have problem with frontend caching?

we make changes to html/js, but we have to clear cache in our browsers to see them. this happens generally for modals and dropdown menus. we use Angularjs/nginx if that's relevant at all.

when developing it's trivial to refresh, but we can't force all users to clear cache every time we update...any ideas?
Prev 1 598 599 600 601 602 1032 Next
Please log in or register to reply.
Live Events Refresh
BSL 21
01:00
Open Quali #2
ZZZero.O72
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SortOf 163
RuFF_SC2 139
Nina 101
Nathanias 100
NeuroSwarm 93
StarCraft: Brood War
Artosis 631
scan(afreeca) 73
ZZZero.O 72
Movie 44
Bale 38
Icarus 7
Dota 2
monkeys_forever510
XaKoH 313
LuMiX0
League of Legends
JimRising 760
Counter-Strike
m0e_tv355
PGG 157
Super Smash Bros
C9.Mang0634
hungrybox544
Other Games
summit1g11899
WinterStarcraft341
Skadoodle179
Maynarde122
Models3
Organizations
Other Games
gamesdonequick1135
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• Berry_CruncH124
• practicex 11
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Lourlo758
• Stunt273
Upcoming Events
Replay Cast
6h 3m
BASILISK vs Shopify Rebellion
Team Liquid vs Team Falcon
OSC
8h 3m
CrankTV Team League
9h 3m
Shopify Rebellion vs Team Liquid
BASILISK vs Team Falcon
Replay Cast
19h 3m
The PondCast
1d 5h
CrankTV Team League
1d 9h
Replay Cast
2 days
WardiTV Invitational
2 days
MaNa vs Gerald
Rogue vs GuMiho
ByuN vs Spirit
herO vs Solar
CrankTV Team League
2 days
Replay Cast
3 days
[ Show More ]
BSL Team A[vengers]
3 days
Dewalt vs Shine
UltrA vs ZeLoT
BSL 21
3 days
Sparkling Tuna Cup
4 days
BSL Team A[vengers]
4 days
Cross vs Motive
Sziky vs HiyA
BSL 21
4 days
Wardi Open
5 days
Monday Night Weeklies
5 days
Liquipedia Results

Completed

CSL 2025 AUTUMN (S18)
WardiTV TLMC #15
Eternal Conflict S1

Ongoing

BSL 21 Points
BSL 21 Team A
C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
CranK Gathers Season 2: SC II Pro Teams
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025

Upcoming

SC4ALL: Brood War
YSL S2
BSL Season 21
SLON Tour Season 2
BSL 21 Non-Korean Championship
RSL Offline Finals
WardiTV 2025
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
META Madness #9
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.