This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
remiX's profile picture

Java help

Started by remiX, 10 April 2013 - 12:44 AM
remiX #1
Posted 10 April 2013 - 02:44 AM
Hey guys.

Does anyone have or can make a function for Java that removes an index from an array?
Like table.remove in lua.

I have tried googling, last night for about 1 hour, but all I could find was removing indexes from lists.
I've also tried making a function which kept causing errors with array index out of bounds which I could find the source of the error ><

Something like
String a[] = {"text","hello","cheese"};
System.out.println(Arrays.toString(a)); // output [text, hello, cheese]

// Not sure how it would work, globally or remove it and then return a new array
removeIndex(a, 2);
// or
a = removeIndex(a, 2);

System.out.println(Arrays.toString(a)); // output [text, cheese]

I did read somewhere that java arrays cannot be changed in any way so I would guess the only way is to make a function.
superaxander #2
Posted 10 April 2013 - 02:47 AM
Can't you just do:

array[i] = null;
GravityScore #3
Posted 10 April 2013 - 02:50 AM
I'm not much of a Java expert, so someone please correct me if I'm wrong :P/>

The way you are currently declaring an array, Java allocates a certain amount of memory to the array, as it knows how many items are in the array. Such as here:

String[] anarray = new String[5]; // 5 items in the array
String[] asecondarray = {"hello", "hello2", "hai", "owiefjoij"}; // 4 items in the array
You can set items to null, or to empty strings, but you can't remove items.

If you want a mutable array, I generally use ArrayList. Basically:

ArrayList<String> anarray = new ArrayList<String>();
anarray.add("hello");
System.out.println(anarray.get(0));
anarray.remove(0);
Not sure how memory efficient an ArrayList is though, and whether there are more memory efficient options. Does anyone know?
superaxander #4
Posted 10 April 2013 - 02:52 AM
I'm not much of a Java expert, so someone please correct me if I'm wrong :P/>/>

The way you are currently declaring an array, Java allocates a certain amount of memory to the array, as it knows how many items are in the array. Such as here:

String[] anarray = new String[5]; // 5 items in the array
String[] asecondarray = {"hello", "hello2", "hai", "owiefjoij"}; // 4 items in the array
You can set items to null, or to empty strings, but you can't remove items.

If you want a mutable array, I generally use ArrayList. Basically:

ArrayList<String> anarray = new ArrayList<String>();
anarray.add("hello");
System.out.println(anarray.get(0));
anarray.remove(0);
Not sure how memory efficient an ArrayList is though, and whether there are more memory efficient options. Does anyone know?
Yeh that's much better
Engineer #5
Posted 10 April 2013 - 02:54 AM
Made an 'function' to do it, at least I think it will:

public String[] name(int index, String[] z ){
	if(index > z.length){
		return z;
	} else {
		String[] returnThis = new String[z.length - 1];
		int newIndex = 0;
		for(int i = 0; i < z.length; i++){
			if(i != index){
				returnThis[newIndex] = z[i];
				newIndex++;
			}
		}
		return returnThis; 
	}
}

Im not that good with Java, so dont blame me if it doesnt even work :P/>/>
remiX #6
Posted 10 April 2013 - 02:56 AM
Should have stated why I don't just make it null.
I currently just set it to "" but I want to have it removed so when I want to access the table I know which number to use, because currently I have tables with 3 index set to "".
I guess it's just for neatness :P/>

I know you can't remove items, which is why I asked if anyone could make/knows about a function that creates a new array and doesn't include the index of the old one which mustn't be included.

I have heard of array list but I found a Split function that returns an array, and me being a java noob - I have no idea what to change to make it return an arraylist…

     private String[] split(String original, String separator) {
        Vector nodes = new Vector();
        // Parse nodes into vector
        int index = original.indexOf(separator);
        while(index>=0) {
            nodes.addElement( original.substring(0, index) );
            original = original.substring(index+separator.length());
            index = original.indexOf(separator);
        }
        // Get the last node
        nodes.addElement( original );

        // Create splitted string array
        String[] result = new String[ nodes.size() ];
        if( nodes.size()>0 ) {
            for(int loop=0; loop<nodes.size(); loop++) {
                result[loop] = (String)nodes.elementAt(loop);
            }
        }
        return result;
    }
theoriginalbit #7
Posted 10 April 2013 - 03:01 AM
I did read somewhere that java arrays cannot be changed in any way so I would guess the only way is to make a function.
Arrays are not immutable, so your method would have to make a new one. use an ArrayList as Grav suggested.
remiX #8
Posted 10 April 2013 - 03:13 AM
Engineer's function works
Thanks again:P
GravityScore #9
Posted 10 April 2013 - 04:08 AM
Uhhh... you know there's a standard split function in the Java string library right?

Why did you need to write your own?

And if you need to convert an array of strings (like the split function returns), there's a toArray method of ArrayList.
remiX #10
Posted 10 April 2013 - 05:00 AM

Oh my, my googling skills are bad. :P/>

I did search for a built in java split function, but the result I got said there was no split function.

Thanks for showing me this :P/>