Given array:
int[] dimension = { 1, 2, 3 };

That produces the output:
1
2
3
12
13
23
123

I'm new to array...i can't figure out the way on even how to start...
any idea?...really appreaciate if someone could help...thanks!

Recommended Answers

All 13 Replies

You have two challenges - first to think through the logical process you need, second to translate that into Java code. You can't start the second until you've completed the first.
So the best thing you can do is to set the PC aside, grab a pencil and pad of paper, and work out how you would do this manually.

You have two challenges - first to think through the logical process you need, second to translate that into Java code. You can't start the second until you've completed the first.
So the best thing you can do is to set the PC aside, grab a pencil and pad of paper, and work out how you would do this manually.

Don't you think this is a little difficult for a beginner?

@javaAddict
I think it is not really too difficult because a beginner should be taught about the thinking process before starting to code. Or may be they have changed the way they teach in school about how to do the programming??? :(

Don't you think this is a little difficult for a beginner?

If you are referring to the project itself - then yes, I agree with you. But then I didn't set the homework.
If you're referring to the process I suggested then I have to disagree - it's a lot more difficult to write the code if you don't understand the procedure you are trying to implement.
Or was I missing a <sarcasm> tag?

any coding logic i should focus in?...struggle whole day also can't figure out dis question...i thought it was easy when get dis question...but when i try to do it...nightmare...i didn't sleep for a nite juz to try to figure out how too make it...thanks to JamesCherrill, javaAddict, Taywin...appreciate u guys help!

I was just thinking about this while I was doing some gardening (a very good time to think). I
I'm gonna guess that there is a decent recursive algorithm, but I can't wrap my head around it yet. This is really hard!

OK, after a quick shower I got a recursive algorithm. It's very small, but ridiculously too hard for a beginner to create.
Are we thinking of this in the wrong way?
If this is a first array exercise for a newbie, then maybe it's just an exercise in using array indexes, and the desired answer is just 7 hard-coded print statements???

all possible dimension, combinations for outPut (my view), because withOut any restrictions are follows:

1
2
3
12
13
21
23
31
32
123
132
213
231
312
321

can help me on the explanation on the coding flow?...i get ans frm somewhere for the abv question?...thanks

import java.util.*;

class Example{
static ArrayList list = new ArrayList();
    public static void generate(String str){
        list.add(str);

        if (str.length() == 1){
            list.add(str);
            return;
        }
        for (int i = 0; i < str.length(); i++){
            generate(str.substring(0,i) + str.substring(i+1));
        }
    }
    public static void main(String args[])
    {
        int arr[]={1,2,3};
        String st ="";
        for(int i=0;i<arr.length;i++){
            st+=Integer.toString(arr[i]);
        }

        generate(st);
        HashSet set=new HashSet(list);
        ArrayList l=new ArrayList(set);
        for(int i=l.size()-1;i>=0;i--){
        System.out.println(l.get(i));
        }
    }
}

The code does nothing but chop the string into ArrayList element... Also, the bottom portion looks jiberish to me... Looks like you try to use it without knowing what it does.

A good example of why not to copy/paste homework from the web!
Did you try running it? Did it produce the correct output? Because if not there is absolutely no point in spending any time with it.

I run that code and seems that it does the trick. Did you run it kaneyip ?

It works for me too.
It starts with "123" then removes chars one at a time to give "23", "12" and "13"
It then recursively removes chars from those substrings to give "2", "3", "1", "2" ...
This gives all the desired substrings, but with duplicates.
It then loads these into a HashSet, which removes the duplicates.
It then converts back to an ArrayList to print the remaining elements.
So, a bit tortuous, but functional.
It is written for pre 1.5 Java, raw types in the Collections and the old-style for loops.

ps: Not really based on arrays - as implied by the OP's requirement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.