• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 05:44
CEST 11:44
KST 18:44
  • 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
Team TLMC #5 - Finalists & Open Tournaments0[ASL20] Ro16 Preview Pt2: Turbulence10Classic Games #3: Rogue vs Serral at BlizzCon9[ASL20] Ro16 Preview Pt1: Ascent10Maestros of the Game: Week 1/Play-in Preview12
Community News
BSL 2025 Warsaw LAN + Legends Showmatch0Weekly Cups (Sept 8-14): herO & MaxPax split cups4WardiTV TL Team Map Contest #5 Tournaments1SC4ALL $6,000 Open LAN in Philadelphia8Weekly Cups (Sept 1-7): MaxPax rebounds & Clem saga continues29
StarCraft 2
General
#1: Maru - Greatest Players of All Time Weekly Cups (Sept 8-14): herO & MaxPax split cups Team Liquid Map Contest #21 - Presented by Monster Energy SpeCial on The Tasteless Podcast Team TLMC #5 - Finalists & Open Tournaments
Tourneys
Maestros of The Game—$20k event w/ live finals in Paris Sparkling Tuna Cup - Weekly Open Tournament SC4ALL $6,000 Open LAN in Philadelphia WardiTV TL Team Map Contest #5 Tournaments RSL: Revival, a new crowdfunded tournament series
Strategy
Custom Maps
External Content
Mutation # 491 Night Drive Mutation # 490 Masters of Midnight Mutation # 489 Bannable Offense Mutation # 488 What Goes Around
Brood War
General
Soulkey on ASL S20 ASL TICKET LIVE help! :D BW General Discussion NaDa's Body A cwal.gg Extension - Easily keep track of anyone
Tourneys
[ASL20] Ro16 Group D [ASL20] Ro16 Group C [Megathread] Daily Proleagues BSL 2025 Warsaw LAN + Legends Showmatch
Strategy
Simple Questions, Simple Answers Muta micro map competition Fighting Spirit mining rates [G] Mineral Boosting
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile Borderlands 3 General RTS Discussion Thread
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Canadian Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread UK Politics Mega-thread
Fan Clubs
The Happy Fan Club!
Media & Entertainment
Movie Discussion! [Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023
World Cup 2022
Tech Support
Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread High temperatures on bridge(s)
TL Community
BarCraft in Tokyo Japan for ASL Season5 Final The Automated Ban List
Blogs
I <=> 9
KrillinFromwales
The Personality of a Spender…
TrAiDoS
A very expensive lesson on ma…
Garnet
hello world
radishsoup
Lemme tell you a thing o…
JoinTheRain
RTS Design in Hypercoven
a11
Evil Gacha Games and the…
ffswowsucks
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1572 users

The Big Programming Thread - Page 600

Forum Index > General Forum
Post a Reply
Prev 1 598 599 600 601 602 1031 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 1031 Next
Please log in or register to reply.
Live Events Refresh
Next event in 16m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Rex 2
StarCraft: Brood War
Calm 5699
Bisu 731
Hyuk 162
HiyA 96
Hyun 96
sorry 86
ToSsGirL 84
Dewaltoss 82
Pusan 77
Light 74
[ Show more ]
Soma 60
actioN 56
Mini 49
ZerO 32
BeSt 30
Nal_rA 28
soO 27
Liquid`Ret 26
Sharp 24
Rush 19
Free 16
SilentControl 10
Dota 2
singsing1508
XcaliburYe237
boxi98170
League of Legends
JimRising 381
Counter-Strike
olofmeister1583
shoxiejesuss629
allub166
Other Games
XaKoH 143
NeuroSwarm75
Trikslyr15
Organizations
Other Games
gamesdonequick599
StarCraft: Brood War
lovetv 593
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 14 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• iopq 1
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos1421
• Stunt679
Other Games
• WagamamaTV81
Upcoming Events
RSL Revival
16m
Maru vs Reynor
Cure vs TriGGeR
Rex2
Map Test Tournament
1h 16m
The PondCast
3h 16m
RSL Revival
1d
Zoun vs Classic
Korean StarCraft League
1d 17h
BSL Open LAN 2025 - War…
1d 22h
RSL Revival
2 days
BSL Open LAN 2025 - War…
2 days
RSL Revival
3 days
Online Event
3 days
[ Show More ]
Wardi Open
4 days
Monday Night Weeklies
4 days
Sparkling Tuna Cup
5 days
LiuLi Cup
6 days
Liquipedia Results

Completed

Proleague 2025-09-10
Chzzk MurlocKing SC1 vs SC2 Cup #2
HCC Europe

Ongoing

BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
LASL Season 20
RSL Revival: Season 2
Maestros of the Game
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1

Upcoming

2025 Chongqing Offline CUP
BSL World Championship of Poland 2025
IPSL Winter 2025-26
BSL Season 21
SC4ALL: Brood War
BSL 21 Team A
Stellar Fest
SC4ALL: StarCraft II
EC S1
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
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.