• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 04:35
CET 10:35
KST 18:35
  • 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
herO wins SC2 All-Star Invitational5SC2 All-Star Invitational: Tournament Preview5RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0
Community News
Weekly Cups (Jan 12-18): herO, MaxPax, Solar win0BSL Season 2025 - Full Overview and Conclusion8Weekly Cups (Jan 5-11): Clem wins big offline, Trigger upsets4$21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)16Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns7
StarCraft 2
General
herO wins SC2 All-Star Invitational PhD study /w SC2 - help with a survey! SC2 Spotted on the EWC 2026 list? Starcraft 2 will not be in the Esports World Cup When will we find out if there are more tournament
Tourneys
$70 Prize Pool Ladder Legends Academy Weekly Open! SC2 All-Star Invitational: Jan 17-18 Sparkling Tuna Cup - Weekly Open Tournament SC2 AI Tournament 2026 $21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)
Strategy
Simple Questions Simple Answers
Custom Maps
[A] Starcraft Sound Mod
External Content
Mutation # 509 Doomsday Report Mutation # 508 Violent Night Mutation # 507 Well Trained Mutation # 506 Warp Zone
Brood War
General
Gypsy to Korea BW General Discussion [ASL21] Potential Map Candidates BGH Auto Balance -> http://bghmmr.eu/ BSL Season 2025 - Full Overview and Conclusion
Tourneys
[Megathread] Daily Proleagues [BSL21] Non-Korean Championship - Starts Jan 10 Small VOD Thread 2.0 Azhi's Colosseum - Season 2
Strategy
Current Meta Simple Questions, Simple Answers Soma's 9 hatch build from ASL Game 2 Game Theory for Starcraft
Other Games
General Games
Nintendo Switch Thread Battle Aces/David Kim RTS Megathread Stormgate/Frost Giant Megathread Beyond All Reason Awesome Games Done Quick 2026!
Dota 2
Official 'what is Dota anymore' discussion
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
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas
Community
General
US Politics Mega-thread Canadian Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread NASA and the Private Sector
Fan Clubs
The herO Fan Club! The IdrA Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece
Sports
2024 - 2026 Football Thread
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Navigating the Risks and Rew…
TrAiDoS
My 2025 Magic: The Gathering…
DARKING
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
James Bond movies ranking - pa…
Topin
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2752 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
Next event in 25m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SortOf 150
StarCraft: Brood War
actioN 713
Hyun 208
EffOrt 131
Killer 109
Shine 98
JulyZerg 88
Hm[arnc] 86
Shinee 73
Mong 69
Mini 65
[ Show more ]
Movie 61
Shuttle 35
Mind 31
Sacsri 30
Nal_rA 23
HiyA 20
Bale 17
zelot 13
ajuk12(nOOB) 12
NotJumperer 11
GoRush 10
Dota 2
XcaliburYe62
League of Legends
JimRising 971
C9.Mang0428
Counter-Strike
olofmeister1121
shoxiejesuss871
allub161
Other Games
summit1g6908
ceh9466
XaKoH 183
Pyrionflax133
Mew2King100
Organizations
Other Games
gamesdonequick1051
StarCraft: Brood War
UltimateBattle 34
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• Berry_CruncH249
• LUISG 25
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Laughngamez YouTube
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 2
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos984
• Stunt405
Upcoming Events
The PondCast
25m
OSC
1h 25m
Clem vs Cure
ByuN vs TBD
TBD vs Solar
MaxPax vs TBD
Krystianer vs TBD
ShoWTimE vs TBD
Big Brain Bouts
2 days
Serral vs TBD
BSL 21
3 days
BSL 21
4 days
Wardi Open
5 days
Monday Night Weeklies
5 days
WardiTV Invitational
6 days
Liquipedia Results

Completed

Proleague 2026-01-20
SC2 All-Star Inv. 2025
NA Kuram Kup

Ongoing

C-Race Season 1
BSL 21 Non-Korean Championship
CSL 2025 WINTER (S19)
KCM Race Survival 2026 Season 1
OSC Championship Season 13
Underdog Cup #3
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025

Upcoming

Escore Tournament S1: W5
Acropolis #4 - TS4
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Rongyi Cup S3
Nations Cup 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
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.