• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 14:08
CET 19:08
KST 03:08
  • 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
ByuL: The Forgotten Master of ZvT29Behind the Blue - Team Liquid History Book19Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13Rongyi Cup S3 - Preview & Info8
Community News
Blizzard Classic Cup - Tastosis announced as captains10Weekly Cups (March 2-8): ByuN overcomes PvT block4GSL CK - New online series18BSL Season 224Vitality ends partnership with ONSYDE20
StarCraft 2
General
Team Liquid Map Contest - Preparation Notice SC2 Spotted on the EWC 2026 list? Terran AddOns placement Blizzard Classic Cup - Tastosis announced as captains GSL CK - New online series
Tourneys
[GSL CK] Team Maru vs. Team herO WardiTV Team League Season 10 Master Swan Open (Global Bronze-Master 2) RSL Season 4 announced for March-April Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
Publishing has been re-enabled! [Feb 24th 2026] Map Editor closed ?
External Content
The PondCast: SC2 News & Results Mutation # 516 Specter of Death Mutation # 515 Together Forever Mutation # 514 Ulnar New Year
Brood War
General
ASL21 General Discussion BW General Discussion BGH Auto Balance -> http://bghmmr.eu/ Gypsy to Korea Are you ready for ASL 21? Hype VIDEO
Tourneys
[Megathread] Daily Proleagues IPSL Spring 2026 is here! ASL Season 21 Qualifiers March 7-8 BWCL Season 64 Announcement
Strategy
Simple Questions, Simple Answers Soma's 9 hatch build from ASL Game 2 Fighting Spirit mining rates Zealot bombing is no longer popular?
Other Games
General Games
Nintendo Switch Thread PC Games Sales Thread Path of Exile No Man's Sky (PS4 and PC) Stormgate/Frost Giant Megathread
Dota 2
Official 'what is Dota anymore' discussion The Story of Wings Gaming
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
Five o'clock TL Mafia Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia TL Mafia Community Thread
Community
General
NASA and the Private Sector US Politics Mega-thread Russo-Ukrainian War Thread Mexico's Drug War Things Aren’t Peaceful in Palestine
Fan Clubs
The IdrA Fan Club
Media & Entertainment
Movie Discussion! [Req][Books] Good Fantasy/SciFi books [Manga] One Piece
Sports
Formula 1 Discussion 2024 - 2026 Football Thread General nutrition recommendations Cricket [SPORT] TL MMA Pick'em Pool 2013
World Cup 2022
Tech Support
Laptop capable of using Photoshop Lightroom?
TL Community
The Automated Ban List
Blogs
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
Gaming-Related Deaths
TrAiDoS
Unintentional protectionism…
Uldridge
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1634 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
WardiTV Team League
12:00
Group B
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
UpATreeSC 148
elazer 56
JuggernautJason55
Livibee 14
StarCraft: Brood War
Britney 34729
Jaedong 1395
Mini 749
EffOrt 560
Larva 396
BeSt 319
firebathero 220
Rush 103
Dewaltoss 101
actioN 59
[ Show more ]
sSak 25
Bale 24
Rock 23
scan(afreeca) 21
ajuk12(nOOB) 12
NaDa 8
Dota 2
Gorgc5749
qojqva2119
Counter-Strike
byalli383
Heroes of the Storm
Khaldor83
MindelVK7
Other Games
Grubby2074
FrodaN973
Beastyqt642
ToD144
Fuzer 140
ArmadaUGS139
KnowMe104
C9.Mang099
Trikslyr56
Organizations
Dota 2
PGL Dota 2 - Main Stream16251
Other Games
gamesdonequick1382
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• StrangeGG 66
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis9393
• TFBlade981
• Shiphtur225
Other Games
• imaqtpie643
Upcoming Events
Replay Cast
5h 52m
Replay Cast
1d 5h
CranKy Ducklings
1d 15h
RSL Revival
1d 15h
WardiTV Team League
1d 17h
uThermal 2v2 Circuit
1d 22h
Patches Events
1d 22h
BSL
2 days
Sparkling Tuna Cup
2 days
RSL Revival
2 days
[ Show More ]
WardiTV Team League
2 days
BSL
3 days
Replay Cast
3 days
Replay Cast
3 days
Wardi Open
3 days
Monday Night Weeklies
3 days
WardiTV Team League
4 days
GSL
5 days
The PondCast
6 days
WardiTV Team League
6 days
Liquipedia Results

Completed

Proleague 2026-03-11
WardiTV Winter 2026
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
Jeongseon Sooper Cup
BSL Season 22
RSL Revival: Season 4
Nations Cup 2026
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual

Upcoming

CSL Elite League 2026
ASL Season 21
Acropolis #4 - TS6
2026 Changsha Offline CUP
Acropolis #4
IPSL Spring 2026
CSLAN 4
HSC XXIX
uThermal 2v2 2026 Main Event
NationLESS Cup
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
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 © 2026 TLnet. All Rights Reserved.